Skip to content

Commit

Permalink
Use more proper throwsAsync & notThrowsAsync assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Dec 25, 2019
1 parent 00df737 commit 64c9181
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 68 deletions.
114 changes: 55 additions & 59 deletions packages/babel/test/as-input-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,12 @@ test('generates sourcemap by default', async (t) => {
});

test('works with proposal-decorators (#18)', async (t) => {
await rollup({
input: 'fixtures/proposal-decorators/main.js',
plugins: [babelPlugin({ babelHelpers: 'bundled' })]
});
t.pass();
await t.notThrowsAsync(() =>
rollup({
input: 'fixtures/proposal-decorators/main.js',
plugins: [babelPlugin({ babelHelpers: 'bundled' })]
})
);
});

test('checks config per-file', async (t) => {
Expand Down Expand Up @@ -199,16 +200,12 @@ export default Foo;
});

test('allows transform-runtime to be used instead of bundled helpers, but throws when CommonJS is used', async (t) => {
try {
await generate('fixtures/runtime-helpers-commonjs/main.js', { babelHelpers: 'runtime' });
t.fail();
} catch (error) {
t.true(
error.message.includes(
'Rollup requires that your Babel configuration keeps ES6 module syntax intact.'
)
);
}
await t.throwsAsync(
() => generate('fixtures/runtime-helpers-commonjs/main.js', { babelHelpers: 'runtime' }),
{
message: /Rollup requires that your Babel configuration keeps ES6 module syntax intact/
}
);
});

test('allows using external-helpers plugin in combination with @babel/plugin-external-helpers', async (t) => {
Expand Down Expand Up @@ -242,11 +239,12 @@ test('correctly renames helpers (#22)', async (t) => {
});

test('runs preflight check correctly in absence of class transformer (#23)', async (t) => {
await rollup({
input: 'fixtures/no-class-transformer/main.js',
plugins: [babelPlugin({ babelHelpers: 'bundled' })]
});
t.pass();
await t.notThrowsAsync(() =>
rollup({
input: 'fixtures/no-class-transformer/main.js',
plugins: [babelPlugin({ babelHelpers: 'bundled' })]
})
);
});

test('produces valid code with typeof helper', async (t) => {
Expand Down Expand Up @@ -289,58 +287,56 @@ test('transpiles only files with whitelisted extensions', async (t) => {
});

test('throws when trying to add babel helper unavailable in used @babel/core version', async (t) => {
try {
await generate('fixtures/basic/main.js', {
plugins: [
function testPlugin() {
return {
visitor: {
Program(path, state) {
state.file.addHelper('__nonexistentHelper');
await t.throwsAsync(
() =>
generate('fixtures/basic/main.js', {
plugins: [
function testPlugin() {
return {
visitor: {
Program(path, state) {
state.file.addHelper('__nonexistentHelper');
}
}
}
};
}
]
});
t.fail();
} catch (err) {
t.is(
err.message,
`${nodePath.resolve(
};
}
]
}),
{
message: `${nodePath.resolve(
__dirname,
'fixtures',
'basic',
'main.js'
)}: Unknown helper __nonexistentHelper`
);
}
}
);
});

test('works with minified bundled helpers', async (t) => {
const BASE_CHAR_CODE = 'a'.charCodeAt(0);
let counter = 0;

await generate('fixtures/class/main.js', {
plugins: [
function testPlugin({ types }) {
return {
visitor: {
FunctionDeclaration(path) {
// super simple mangling
path
.get('id')
.replaceWith(types.identifier(String.fromCharCode(BASE_CHAR_CODE + counter)));

counter += 1;
}
}
};
}
]
});
await t.notThrowsAsync(() =>
generate('fixtures/class/main.js', {
plugins: [
function testPlugin({ types }) {
return {
visitor: {
FunctionDeclaration(path) {
// super simple mangling
path
.get('id')
.replaceWith(types.identifier(String.fromCharCode(BASE_CHAR_CODE + counter)));

t.pass();
counter += 1;
}
}
};
}
]
})
);
});

test('supports customizing the loader', async (t) => {
Expand Down
12 changes: 3 additions & 9 deletions packages/babel/test/as-output-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,9 @@ test('supports customizing the loader', async (t) => {
});

test('throws when using a Rollup output format other than esm or cjs', async (t) => {
try {
await generate('fixtures/basic/main.js', {}, { format: 'iife' });
t.fail('Rollup did not throw');
} catch (error) {
t.is(
error.message,
`Using Babel on the generated chunks is strongly discouraged for formats other than "esm" or "cjs" as it can easily break wrapper code and lead to accidentally created global variables. Instead, you should set "output.format" to "esm" and use Babel to transform to another format, e.g. by adding "presets: [['@babel/env', { modules: 'umd' }]]" to your Babel options. If you still want to proceed, add "allowAllFormats: true" to your plugin options.`
);
}
await t.throwsAsync(() => generate('fixtures/basic/main.js', {}, { format: 'iife' }), {
message: `Using Babel on the generated chunks is strongly discouraged for formats other than "esm" or "cjs" as it can easily break wrapper code and lead to accidentally created global variables. Instead, you should set "output.format" to "esm" and use Babel to transform to another format, e.g. by adding "presets: [['@babel/env', { modules: 'umd' }]]" to your Babel options. If you still want to proceed, add "allowAllFormats: true" to your plugin options.`
});
});

test('allows using a Rollup output format other than esm or cjs with allowAllFormats', async (t) => {
Expand Down

0 comments on commit 64c9181

Please sign in to comment.