Skip to content

Commit

Permalink
Use correct properties per test type
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Apr 30, 2023
1 parent 6edc9eb commit 9db29e0
Show file tree
Hide file tree
Showing 19 changed files with 406 additions and 253 deletions.
4 changes: 2 additions & 2 deletions test/browser/define.d.ts
@@ -1,5 +1,5 @@
import type { TestConfigBase } from '../types';
import type { TestConfigBrowser } from '../types';

declare global {
function defineTest(config: TestConfigBase): TestConfigBase;
function defineTest(config: TestConfigBrowser): TestConfigBrowser;
}
101 changes: 54 additions & 47 deletions test/browser/index.js
Expand Up @@ -13,58 +13,65 @@ const fixturify = require('fixturify');
const { rollup } = require('../../browser/dist/rollup.browser.js');
const { assertFilesAreEqual, runTestSuiteWithSamples, compareError } = require('../utils.js');

runTestSuiteWithSamples('browser', resolve(__dirname, 'samples'), (directory, config) => {
(config.skip ? it.skip : config.solo ? it.only : it)(
basename(directory) + ': ' + config.description,
async () => {
let bundle;
try {
bundle = await rollup({
input: 'main',
onwarn: warning => {
if (!(config.expectedWarnings && config.expectedWarnings.includes(warning.code))) {
throw new Error(
`Unexpected warnings (${warning.code}): ${warning.message}\n` +
'If you expect warnings, list their codes in config.expectedWarnings'
);
}
},
strictDeprecations: true,
...config.options
});
} catch (error) {
runTestSuiteWithSamples(
'browser',
resolve(__dirname, 'samples'),
/**
* @param {import('../types').TestConfigBrowser} config
*/
(directory, config) => {
(config.skip ? it.skip : config.solo ? it.only : it)(
basename(directory) + ': ' + config.description,
async () => {
let bundle;
try {
bundle = await rollup({
input: 'main',
onwarn: warning => {
if (!(config.expectedWarnings && config.expectedWarnings.includes(warning.code))) {
throw new Error(
`Unexpected warnings (${warning.code}): ${warning.message}\n` +
'If you expect warnings, list their codes in config.expectedWarnings'
);
}
},
strictDeprecations: true,
...config.options
});
} catch (error) {
if (config.error) {
compareError(error, config.error);
return;
} else {
throw error;
}
}
if (config.error) {
compareError(error, config.error);
return;
} else {
throw error;
throw new Error('Expected an error while rolling up');
}
let output;
try {
({ output } = await bundle.generate({
exports: 'auto',
format: 'es',
...(config.options || {}).output
}));
} catch (error) {
if (config.generateError) {
compareError(error, config.generateError);
return;
} else {
throw error;
}
}
}
if (config.error) {
throw new Error('Expected an error while rolling up');
}
let output;
try {
({ output } = await bundle.generate({
exports: 'auto',
format: 'es',
...(config.options || {}).output
}));
} catch (error) {
if (config.generateError) {
compareError(error, config.generateError);
return;
} else {
throw error;
throw new Error('Expected an error while generating output');
}
assertOutputMatches(output, directory);
}
if (config.generateError) {
throw new Error('Expected an error while generating output');
}
assertOutputMatches(output, directory);
}
);
});
);
}
);

function assertOutputMatches(output, directory) {
/** @type any */
Expand Down
99 changes: 54 additions & 45 deletions test/chunking-form/index.js
Expand Up @@ -8,55 +8,64 @@ const { runTestSuiteWithSamples, assertDirectoriesAreEqual } = require('../utils

const FORMATS = ['es', 'cjs', 'amd', 'system'];

runTestSuiteWithSamples('chunking form', resolve(__dirname, 'samples'), (directory, config) => {
(config.skip ? describe.skip : config.solo ? describe.only : describe)(
basename(directory) + ': ' + config.description,
() => {
let bundle;
runTestSuiteWithSamples(
'chunking form',
resolve(__dirname, 'samples'),
/**
* @param {import('../types').TestConfigChunkingForm} config
*/
(directory, config) => {
(config.skip ? describe.skip : config.solo ? describe.only : describe)(
basename(directory) + ': ' + config.description,
async () => {
let bundle;

if (config.before) {
before(config.before);
}
if (config.after) {
after(config.after);
}
if (config.before) {
await before(config.before);
}
if (config.after) {
await after(config.after);
}

for (const format of FORMATS) {
it('generates ' + format, async () => {
chdir(directory);
bundle =
bundle ||
(await rollup({
input: [directory + '/main.js'],
onwarn: warning => {
if (!(config.expectedWarnings && config.expectedWarnings.includes(warning.code))) {
throw new Error(
`Unexpected warnings (${warning.code}): ${warning.message}\n` +
'If you expect warnings, list their codes in config.expectedWarnings'
);
}
for (const format of FORMATS) {
it('generates ' + format, async () => {
chdir(directory);
bundle =
bundle ||
(await rollup({
input: [directory + '/main.js'],
onwarn: warning => {
if (
!(config.expectedWarnings && config.expectedWarnings.includes(warning.code))
) {
throw new Error(
`Unexpected warnings (${warning.code}): ${warning.message}\n` +
'If you expect warnings, list their codes in config.expectedWarnings'
);
}
},
strictDeprecations: true,
...config.options
}));
await generateAndTestBundle(
bundle,
{
dir: `${directory}/_actual/${format}`,
exports: 'auto',
format,
chunkFileNames: 'generated-[name].js',
validate: true,
...(config.options || {}).output
},
strictDeprecations: true,
...config.options
}));
await generateAndTestBundle(
bundle,
{
dir: `${directory}/_actual/${format}`,
exports: 'auto',
format,
chunkFileNames: 'generated-[name].js',
validate: true,
...(config.options || {}).output
},
`${directory}/_expected/${format}`,
config
);
});
`${directory}/_expected/${format}`,
config
);
});
}
}
}
);
});
);
}
);

async function generateAndTestBundle(bundle, outputOptions, expectedDirectory, config) {
await bundle.write({
Expand Down
@@ -1,5 +1,6 @@
const assert = require('node:assert');
const path = require('node:path');
// @ts-expect-error not included in types
const { getObject } = require('../../../../utils');

module.exports = defineTest({
Expand Down
8 changes: 5 additions & 3 deletions test/cli/index.js
Expand Up @@ -57,8 +57,10 @@ async function runTest(config, command) {
env: { ...process.env, FORCE_COLOR: '0', ...config.env },
killSignal: 'SIGKILL'
},
(error, code, stderr) => {
if (config.after) config.after(error, code, stderr);
async (error, code, stderr) => {
if (config.after) {
await config.after(error, code, stderr);
}
if (error && !error.killed) {
if (config.error) {
if (!config.error(error)) {
Expand Down Expand Up @@ -94,7 +96,7 @@ async function runTest(config, command) {
}

if (config.exports) {
config.exports(module.exports);
await config.exports(module.exports);
}
} catch (error) {
if (config.error) {
Expand Down
1 change: 1 addition & 0 deletions test/cli/samples/config-type-module/_config.js
@@ -1,3 +1,4 @@
// @ts-expect-error not included in types
const { assertIncludes } = require('../../../utils.js');

module.exports = defineTest({
Expand Down
2 changes: 2 additions & 0 deletions test/file-hashes/index.js
Expand Up @@ -2,7 +2,9 @@ const path = require('node:path');
/**
* @type {import('../../src/rollup/types')} Rollup
*/
// @ts-expect-error not included in types
const rollup = require('../../dist/rollup');
// @ts-expect-error not included in types
const { runTestSuiteWithSamples } = require('../utils.js');

runTestSuiteWithSamples(
Expand Down

0 comments on commit 9db29e0

Please sign in to comment.