Skip to content

Commit

Permalink
test: enable typecheck for _config files (#4954)
Browse files Browse the repository at this point in the history
* chore: init

* chore: basic

* chore: revert vscode config

* chore: fix syntax error

* chore: improve typecheck

* feat: different types based on dirs

* chore: update

* chore: update

* fix: fix some type error

* chore: disable check js for now

* chore: update

* chore: update

* chore: fix emission issue in WebStorm

* Fix some type issues

* Use correct properties per test type

---------

Co-authored-by: Lukas Taegert-Atkinson <lukas.taegert-atkinson@tngtech.com>
  • Loading branch information
antfu and lukastaegert committed May 1, 2023
1 parent 4fb9778 commit 64870c3
Show file tree
Hide file tree
Showing 1,978 changed files with 4,943 additions and 4,209 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.js
Expand Up @@ -66,6 +66,12 @@ module.exports = {
'no-undef': 'off',
'unicorn/prevent-abbreviations': 'off'
}
},
{
files: ['test/**/_config.js'],
rules: {
'no-undef': 'off'
}
}
],
parser: '@typescript-eslint/parser',
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -18,3 +18,4 @@ perf/
.github_token
.temp
docs/.vitepress/graphs
*.tsbuildinfo
4 changes: 3 additions & 1 deletion .vscode/settings.json
Expand Up @@ -4,5 +4,7 @@
"typescript.format.enable": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
},
"references.preferredLocation": "peek",
"vue.features.codeActions.enable": false
}
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -81,6 +81,7 @@
"@rollup/plugin-typescript": "^11.1.0",
"@rollup/pluginutils": "^5.0.2",
"@types/estree": "1.0.1",
"@types/mocha": "^10.0.1",
"@types/node": "^14.18.42",
"@types/yargs-parser": "^21.0.0",
"@typescript-eslint/eslint-plugin": "^5.59.1",
Expand Down
2 changes: 1 addition & 1 deletion src/rollup/types.d.ts
Expand Up @@ -519,7 +519,7 @@ export type InputPluginOption = MaybePromise<Plugin | NullValue | false | InputP

export interface InputOptions {
acorn?: Record<string, unknown>;
acornInjectPlugins?: (() => unknown)[] | (() => unknown);
acornInjectPlugins?: ((...arguments_: any[]) => unknown)[] | ((...arguments_: any[]) => unknown);
cache?: boolean | RollupCache;
context?: string;
experimentalCacheExpiry?: number;
Expand Down
5 changes: 5 additions & 0 deletions test/browser/define.d.ts
@@ -0,0 +1,5 @@
import type { TestConfigBrowser } from '../types';

declare global {
function defineTest(config: TestConfigBrowser): TestConfigBrowser;
}
107 changes: 60 additions & 47 deletions test/browser/index.js
@@ -1,67 +1,80 @@
// since we don't run the browser tests in an actual browser, we need to make `performance`
// globally accessible same as in the browser. this can be removed once `performance` is
// available globally in all supported platforms. [currently global for node.js v16+].
// @ts-expect-error ignore
global.performance = require('node:perf_hooks').performance;

const { basename, resolve } = require('node:path');
const fixturify = require('fixturify');

/**
* @type {import('../../src/rollup/types')} Rollup
*/
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 */
const actual = {};
for (const file of output) {
const filePath = file.fileName.split('/');
Expand Down
4 changes: 2 additions & 2 deletions test/browser/samples/basic/_config.js
@@ -1,6 +1,6 @@
const { loader } = require('../../../utils.js');

module.exports = {
module.exports = defineTest({
description: 'bundles files for the browser',
options: {
plugins: loader({
Expand All @@ -9,4 +9,4 @@ console.log(foo);`,
dep: `export const foo = 42;`
})
}
};
});
4 changes: 2 additions & 2 deletions test/browser/samples/missing-dependency-resolution/_config.js
@@ -1,6 +1,6 @@
const { loader } = require('../../../utils.js');

module.exports = {
module.exports = defineTest({
description: 'fails if a dependency cannot be resolved',
options: {
plugins: loader({
Expand All @@ -15,4 +15,4 @@ console.log(foo);`
url: 'https://rollupjs.org/plugin-development/#a-simple-example',
watchFiles: ['main']
}
};
});
4 changes: 2 additions & 2 deletions test/browser/samples/missing-entry-resolution/_config.js
@@ -1,9 +1,9 @@
module.exports = {
module.exports = defineTest({
description: 'fails if an entry cannot be resolved',
error: {
code: 'NO_FS_IN_BROWSER',
message:
'Cannot access the file system (via "path.resolve") when using the browser build of Rollup. Make sure you supply a plugin with custom resolveId and load hooks to Rollup.',
url: 'https://rollupjs.org/plugin-development/#a-simple-example'
}
};
});
5 changes: 3 additions & 2 deletions test/browser/samples/missing-load/_config.js
@@ -1,7 +1,8 @@
module.exports = {
module.exports = defineTest({
description: 'fails if a file cannot be loaded',
options: {
plugins: {
name: 'test',
resolveId(source) {
return source;
}
Expand All @@ -14,4 +15,4 @@ module.exports = {
url: 'https://rollupjs.org/plugin-development/#a-simple-example',
watchFiles: ['main']
}
};
});
5 changes: 3 additions & 2 deletions test/browser/samples/renormalizes-external-paths/_config.js
@@ -1,13 +1,14 @@
const { join, dirname } = require('node:path').posix;

module.exports = {
module.exports = defineTest({
description: 'renormalizes external paths if possible',
options: {
input: ['/main.js', '/nested/entry.js'],
external(id) {
return id.endsWith('ext');
},
plugins: {
name: 'test-plugin',
resolveId(source, importer) {
if (source.endsWith('ext.js')) {
return false;
Expand Down Expand Up @@ -44,4 +45,4 @@ import './nested-ext.js';`;
}
}
}
};
});
4 changes: 2 additions & 2 deletions test/browser/samples/supports-hashes/_config.js
@@ -1,6 +1,6 @@
const { loader } = require('../../../utils.js');

module.exports = {
module.exports = defineTest({
description: 'bundles files for the browser',
options: {
input: ['main', 'dep'],
Expand All @@ -13,4 +13,4 @@ console.log(foo);`,
entryFileNames: '[name]-[hash].js'
}
}
};
});
8 changes: 8 additions & 0 deletions test/browser/tsconfig.json
@@ -0,0 +1,8 @@
{
"extends": "../tsconfig.base.json",
"include": [
"**/_config.js",
"./index.js",
"./define.d.ts"
]
}
5 changes: 5 additions & 0 deletions test/chunking-form/define.d.ts
@@ -0,0 +1,5 @@
import type { TestConfigChunkingForm } from '../types';

declare global {
function defineTest(config: TestConfigChunkingForm): TestConfigChunkingForm;
}
6 changes: 6 additions & 0 deletions test/chunking-form/index.js
@@ -1,5 +1,8 @@
const { basename, resolve } = require('node:path');
const { chdir } = require('node:process');
/**
* @type {import('../../src/rollup/types')} Rollup
*/
const { rollup } = require('../../dist/rollup');
const { runTestSuiteWithSamples, assertDirectoriesAreEqual } = require('../utils.js');

Expand Down Expand Up @@ -63,6 +66,7 @@ async function generateAndTestBundle(bundle, outputOptions, expectedDirectory, c
if (outputOptions.format === 'amd' && config.runAmd) {
try {
const exports = await new Promise((resolve, reject) => {
// @ts-expect-error global
global.assert = require('node:assert');
const requirejs = require('requirejs');
requirejs.config({ baseUrl: outputOptions.dir });
Expand All @@ -73,7 +77,9 @@ async function generateAndTestBundle(bundle, outputOptions, expectedDirectory, c
}
} finally {
delete require.cache[require.resolve('requirejs')];
// @ts-expect-error global
delete global.requirejsVars;
// @ts-expect-error global
delete global.assert;
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/chunking-form/samples/aliasing-extensions/_config.js
@@ -1,6 +1,6 @@
module.exports = {
module.exports = defineTest({
description: 'chunk aliasing with extensions',
options: {
input: ['main1', 'main2', 'main3.ts']
}
};
});
@@ -1,4 +1,4 @@
module.exports = {
module.exports = defineTest({
description: "allows to use amd.autoId with amd.basePath and works when concat'd into one file",
options: {
input: ['main'],
Expand Down Expand Up @@ -31,4 +31,4 @@ module.exports = {
return exports.getA();
}
}
};
});
4 changes: 2 additions & 2 deletions test/chunking-form/samples/amd-id-auto-base-path/_config.js
@@ -1,4 +1,4 @@
module.exports = {
module.exports = defineTest({
description: 'allows to use amd.autoId with amd.basePath, and chunks folder',
options: {
input: ['main'],
Expand All @@ -16,4 +16,4 @@ module.exports = {
return exports.getA();
}
}
};
});
4 changes: 2 additions & 2 deletions test/chunking-form/samples/amd-id-auto/_config.js
@@ -1,4 +1,4 @@
module.exports = {
module.exports = defineTest({
description: 'allows to use amd.autoId',
options: {
input: ['main'],
Expand All @@ -13,4 +13,4 @@ module.exports = {
return exports.getA();
}
}
};
});

0 comments on commit 64870c3

Please sign in to comment.