Skip to content

Commit

Permalink
tests: Migrate to Vitest from Jest. (#236)
Browse files Browse the repository at this point in the history
* Remove jest dep.

* Update tests.

* Update babel plugins.

* Update babel.

* Update swc.

* Update stuff.

* More stuff.

* Add fs layer.

* Start on tests.

* Share fs.

* Update package.

* Get examples working.

* Include wrappers.

* Handle assets via bundle.

* Fix some tests.

* Update outputs.

* Update deps.

* Fix more tests.

* Update docs.

* Fix test.

* Fix script.

* Add preset.

* Fix test.

* More fixes.
  • Loading branch information
milesj committed Feb 21, 2024
1 parent 430b564 commit 04fbfd3
Show file tree
Hide file tree
Showing 101 changed files with 23,569 additions and 10,776 deletions.
4 changes: 3 additions & 1 deletion .moon/tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ tasks:
NODE_ENV: 'production'

test:
command: 'noop' # TODO
command: 'vitest run --config ../../vitest.config.ts'
inputs:
- '/vitest.config.ts'
deps:
- '^:build'

Expand Down
22 changes: 0 additions & 22 deletions babel.config.js

This file was deleted.

13 changes: 0 additions & 13 deletions jest.config.js

This file was deleted.

15 changes: 6 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,25 @@
"devDependencies": {
"@babel/cli": "^7.23.9",
"@babel/core": "^7.23.9",
"@moonrepo/cli": "^1.21.3",
"@moonrepo/cli": "^1.21.4",
"@moonrepo/dev": "^2.0.1",
"@swc/cli": "^0.3.9",
"@types/fs-extra": "^11.0.4",
"@types/jest": "^29.5.12",
"@types/micromatch": "^4.0.6",
"@types/node": "^16.18.80",
"@types/react": "^18.2.55",
"@types/node": "^16.18.82",
"@types/react": "^18.2.57",
"@types/semver": "^7.5.7",
"babel-preset-moon": "^2.0.14",
"chokidar": "^3.6.0",
"electron-to-chromium": "^1.4.665",
"electron-to-chromium": "^1.4.675",
"eslint": "^8.56.0",
"eslint-config-moon": "^2.1.1",
"jest": "^29.7.0",
"jest-preset-moon": "^2.0.7",
"lerna": "^8.1.2",
"prettier": "^3.2.5",
"prettier-config-moon": "^1.1.2",
"ts-node": "^10.9.2",
"tsconfig-moon": "^1.3.0",
"typescript": "^5.3.3"
"typescript": "^5.3.3",
"vitest": "^1.3.0"
},
"funding": {
"type": "ko-fi",
Expand Down
3 changes: 0 additions & 3 deletions packages/babel-plugin-cjs-esm-interop/jest.config.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/babel-plugin-cjs-esm-interop/tests/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, expect, it } from 'vitest';
import { transformAsync, TransformOptions } from '@babel/core';
import cjsEsmInterop, { CjsEsmInteropOptions } from '../src';

Expand Down
11 changes: 0 additions & 11 deletions packages/babel-plugin-conditional-invariant/jest.config.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`conditionalInvariantPlugin() will not transform if wrapped in a matching conditional 1`] = `
exports[`conditionalInvariantPlugin() > will not transform if wrapped in a matching conditional 1`] = `
"if (process.env.NODE_ENV !== 'production') {
invariant();
}"
`;

exports[`conditionalInvariantPlugin() will not transform if wrapped in a matching conditional that is layers deep 1`] = `
exports[`conditionalInvariantPlugin() > will not transform if wrapped in a matching conditional that is layers deep 1`] = `
"if (process.env.NODE_ENV !== 'production') {
if (true) {
if (false) {
Expand All @@ -16,15 +16,15 @@ exports[`conditionalInvariantPlugin() will not transform if wrapped in a matchin
}"
`;

exports[`conditionalInvariantPlugin() will transform if wrapped in a non-matching conditional 1`] = `
exports[`conditionalInvariantPlugin() > will transform if wrapped in a non-matching conditional 1`] = `
"if (process.env.NODE_ENV === 'development') {
if (process.env.NODE_ENV !== 'production') {
invariant();
}
}"
`;

exports[`conditionalInvariantPlugin() will transform if wrapped in a non-matching conditional that is layers deep 1`] = `
exports[`conditionalInvariantPlugin() > will transform if wrapped in a non-matching conditional that is layers deep 1`] = `
"if (process.env.NODE_ENV === 'development') {
if (true) {
if (false) {
Expand All @@ -36,7 +36,7 @@ exports[`conditionalInvariantPlugin() will transform if wrapped in a non-matchin
}"
`;

exports[`conditionalInvariantPlugin() will transform valid expressions 1`] = `
exports[`conditionalInvariantPlugin() > will transform valid expressions 1`] = `
"if (process.env.NODE_ENV !== 'production') {
invariant();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, expect, it } from 'vitest';
import { transformAsync, TransformOptions } from '@babel/core';
import conditionalInvariantPlugin from '../src';

Expand Down
3 changes: 0 additions & 3 deletions packages/babel-plugin-env-constants/jest.config.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`envConstantsPlugin() transforms __DEV__ expressions 1`] = `
exports[`envConstantsPlugin() > transforms __DEV__ expressions 1`] = `
"if (process.env.NODE_ENV !== 'production') {} else if (process.env.NODE_ENV !== 'production' && 123) {} else if (true || process.env.NODE_ENV !== 'production') {} else if (!(process.env.NODE_ENV !== 'production')) {} else {}
switch (process.env.NODE_ENV !== 'production') {}
while (process.env.NODE_ENV !== 'production') {}
Expand All @@ -13,7 +13,7 @@ const arrayValue = [process.env.NODE_ENV !== 'production'];
React.createElement(Foo, null, process.env.NODE_ENV !== 'production' ? 'Child' : null);"
`;

exports[`envConstantsPlugin() transforms __PROD__ expressions 1`] = `
exports[`envConstantsPlugin() > transforms __PROD__ expressions 1`] = `
"if (process.env.NODE_ENV === 'production') {} else if (process.env.NODE_ENV === 'production' && 123) {} else if (true || process.env.NODE_ENV === 'production') {} else if (!(process.env.NODE_ENV === 'production')) {} else {}
switch (process.env.NODE_ENV === 'production') {}
while (process.env.NODE_ENV === 'production') {}
Expand All @@ -26,7 +26,7 @@ const arrayValue = [process.env.NODE_ENV === 'production'];
React.createElement(Foo, null, process.env.NODE_ENV === 'production' ? 'Child' : null);"
`;

exports[`envConstantsPlugin() transforms __TEST__ expressions 1`] = `
exports[`envConstantsPlugin() > transforms __TEST__ expressions 1`] = `
"if (process.env.NODE_ENV === 'test') {} else if (process.env.NODE_ENV === 'test' && 123) {} else if (true || process.env.NODE_ENV === 'test') {} else if (!(process.env.NODE_ENV === 'test')) {} else {}
switch (process.env.NODE_ENV === 'test') {}
while (process.env.NODE_ENV === 'test') {}
Expand All @@ -39,7 +39,7 @@ const arrayValue = [process.env.NODE_ENV === 'test'];
React.createElement(Foo, null, process.env.NODE_ENV === 'test' ? 'Child' : null);"
`;

exports[`envConstantsPlugin() will not transform invalid __DEV__ expressions 1`] = `
exports[`envConstantsPlugin() > will not transform invalid __DEV__ expressions 1`] = `
"const __DEV__ = 123;
const objectProperty = {
__DEV__: true
Expand All @@ -53,7 +53,7 @@ const arrayIndex = [];
arrayIndex[__DEV__] = 1;"
`;

exports[`envConstantsPlugin() will not transform invalid __PROD__ expressions 1`] = `
exports[`envConstantsPlugin() > will not transform invalid __PROD__ expressions 1`] = `
"const __PROD__ = 123;
const objectProperty = {
__PROD__: true
Expand All @@ -67,7 +67,7 @@ const arrayIndex = [];
arrayIndex[__PROD__] = 1;"
`;

exports[`envConstantsPlugin() will not transform invalid __TEST__ expressions 1`] = `
exports[`envConstantsPlugin() > will not transform invalid __TEST__ expressions 1`] = `
"const __TEST__ = 123;
const objectProperty = {
__TEST__: true
Expand Down
1 change: 1 addition & 0 deletions packages/babel-plugin-env-constants/tests/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, expect, it } from 'vitest';
import { transformAsync, TransformOptions } from '@babel/core';
import envConstantsPlugin from '../src';

Expand Down
5 changes: 0 additions & 5 deletions packages/packemon/jest.config.js

This file was deleted.

7 changes: 3 additions & 4 deletions packages/packemon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,17 @@
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@swc/core": "^1.4.0",
"@swc/core": "^1.4.2",
"@swc/helpers": "^0.5.6",
"babel-plugin-cjs-esm-interop": "^3.0.3",
"babel-plugin-conditional-invariant": "^3.0.1",
"babel-plugin-env-constants": "^3.0.1",
"babel-preset-solid": "^1.8.12",
"browserslist": "^4.22.3",
"babel-preset-solid": "^1.8.15",
"browserslist": "^4.23.0",
"debug": "^4.3.4",
"execa": "^8.0.1",
"fast-glob": "^3.3.2",
"filesize": "^10.1.0",
"fs-extra": "^11.2.0",
"ink": "^4.4.1",
"ink-progress-bar": "^3.0.0",
"ink-spinner": "^5.0.0",
Expand Down
21 changes: 16 additions & 5 deletions packages/packemon/src/Artifact.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { execa } from 'execa';
import fs from 'fs-extra';
import { rollup } from 'rollup';
import { applyStyle } from '@boost/cli';
import { isObject, Path, toArray, VirtualPath } from '@boost/common';
Expand All @@ -14,7 +13,7 @@ import type {
Build,
BuildOptions,
BuildResult,
BuildResultFiles,
BuildResultFile,
ConfigFile,
FeatureFlags,
Format,
Expand Down Expand Up @@ -108,7 +107,7 @@ export class Artifact {
},
});

const files: BuildResultFiles[] = [];
const files: BuildResultFile[] = [];

await Promise.all(
toArray(output).map(async (out, index) => {
Expand All @@ -126,6 +125,15 @@ export class Artifact {

// Update build results and stats
const bundledCode = result.output.reduce((code, chunk) => {
if (chunk.type === 'asset') {
files.push({
code: typeof chunk.source === 'string' ? chunk.source : '',
file: chunk.fileName.endsWith('js')
? `${originalFormat}/${chunk.fileName}`
: chunk.fileName,
});
}

if (chunk.type === 'chunk') {
files.push({
code: chunk.code,
Expand Down Expand Up @@ -195,7 +203,7 @@ export class Artifact {
if (hasCjs) {
this.debug('CJS types compatibility enabled, renaming `.d.ts` to `.d.cts`');

await convertCjsTypes(this.package.path.append('cjs'));
await convertCjsTypes(this.package.path.append('cjs'), this.package.fs);
}
}
}
Expand All @@ -206,12 +214,15 @@ export class Artifact {
const dirs = ['assets', 'dts', ...this.builds.map((build) => build.format)];

await Promise.all(
// eslint-disable-next-line @typescript-eslint/require-await
dirs.map(async (dir) => {
const dirPath = this.package.path.append(dir).path();

this.debug(' - %s', dirPath);

await fs.remove(dirPath);
if (this.package.fs.exists(dirPath)) {
this.package.fs.removeDir(dirPath);
}
}),
);
}
Expand Down
32 changes: 32 additions & 0 deletions packages/packemon/src/FileSystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import fs from 'node:fs';
import { json } from '@boost/common';

export interface FileSystem {
copyFile: (from: string, to: string) => void;
createDirAll: (path: string) => void;
exists: (path: string) => boolean;
readFile: (path: string) => string;
readJson: <T>(path: string) => T;
removeDir: (path: string) => void;
removeFile: (path: string) => void;
writeFile: (path: string, data: string) => void;
writeJson: (path: string, data: unknown) => void;
}

export const nodeFileSystem: FileSystem = {
copyFile: fs.copyFileSync,
createDirAll: (path) => fs.mkdirSync(path, { recursive: true }),
exists: (path) => fs.existsSync(path),
readFile: (path) => fs.readFileSync(path, 'utf8'),
readJson: (path) => json.parse(nodeFileSystem.readFile(path)),
removeDir: (path) => {
fs.rmSync(path, { recursive: true });
},
removeFile: fs.unlinkSync,
writeFile: (path, data) => {
fs.writeFileSync(path, `${data.trim()}\n`, 'utf8');
},
writeJson: (path, data) => {
nodeFileSystem.writeFile(path, JSON.stringify(data, null, 2));
},
};
Loading

0 comments on commit 04fbfd3

Please sign in to comment.