Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warning: Fix ESLint warnings in tests #46033

Merged
merged 1 commit into from
Nov 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 60 additions & 56 deletions packages/warning/test/babel-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,88 +12,92 @@ function join( ...strings ) {
return strings.join( '\n' );
}

function compare( input, output, options = {} ) {
function transformCode( input, options = {} ) {
const { code } = transform( input, {
configFile: false,
plugins: [ [ babelPlugin, options ] ],
} );
expect( code ).toEqual( output );
return code;
}

describe( 'babel-plugin', () => {
it( 'should replace warning calls with import declaration', () => {
compare(
join(
'import warning from "@wordpress/warning";',
'warning("a");'
),
join(
'import warning from "@wordpress/warning";',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warning("a") : void 0;'
)
const input = join(
'import warning from "@wordpress/warning";',
'warning("a");'
);
const expected = join(
'import warning from "@wordpress/warning";',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warning("a") : void 0;'
);

expect( transformCode( input ) ).toEqual( expected );
} );

it( 'should not replace warning calls without import declaration', () => {
compare( 'warning("a");', 'warning("a");' );
const input = 'warning("a");';
const expected = 'warning("a");';

expect( transformCode( input ) ).toEqual( expected );
} );

it( 'should replace warning calls without import declaration with plugin options', () => {
compare(
'warning("a");',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warning("a") : void 0;',
{ callee: 'warning' }
);
const input = 'warning("a");';
const options = { callee: 'warning' };
const expected =
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warning("a") : void 0;';

expect( transformCode( input, options ) ).toEqual( expected );
} );

it( 'should replace multiple warning calls', () => {
compare(
join(
'import warning from "@wordpress/warning";',
'warning("a");',
'warning("b");',
'warning("c");'
),
join(
'import warning from "@wordpress/warning";',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warning("a") : void 0;',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warning("b") : void 0;',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warning("c") : void 0;'
)
const input = join(
'import warning from "@wordpress/warning";',
'warning("a");',
'warning("b");',
'warning("c");'
);
const expected = join(
'import warning from "@wordpress/warning";',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warning("a") : void 0;',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warning("b") : void 0;',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warning("c") : void 0;'
);

expect( transformCode( input ) ).toEqual( expected );
} );

it( 'should identify warning callee name', () => {
compare(
join(
'import warn from "@wordpress/warning";',
'warn("a");',
'warn("b");',
'warn("c");'
),
join(
'import warn from "@wordpress/warning";',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warn("a") : void 0;',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warn("b") : void 0;',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warn("c") : void 0;'
)
const input = join(
'import warn from "@wordpress/warning";',
'warn("a");',
'warn("b");',
'warn("c");'
);
const expected = join(
'import warn from "@wordpress/warning";',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warn("a") : void 0;',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warn("b") : void 0;',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warn("c") : void 0;'
);

expect( transformCode( input ) ).toEqual( expected );
} );

it( 'should identify warning callee name by', () => {
compare(
join(
'import warn from "@wordpress/warning";',
'warn("a");',
'warn("b");',
'warn("c");'
),
join(
'import warn from "@wordpress/warning";',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warn("a") : void 0;',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warn("b") : void 0;',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warn("c") : void 0;'
)
const input = join(
'import warn from "@wordpress/warning";',
'warn("a");',
'warn("b");',
'warn("c");'
);
const expected = join(
'import warn from "@wordpress/warning";',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warn("a") : void 0;',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warn("b") : void 0;',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warn("c") : void 0;'
);

expect( transformCode( input ) ).toEqual( expected );
} );
} );