Skip to content

Commit

Permalink
test: migrate from jest to vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
abichinger committed Oct 20, 2024
1 parent 7c936a1 commit fed5dcd
Show file tree
Hide file tree
Showing 16 changed files with 580 additions and 1,800 deletions.
1 change: 0 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"root": true,
"plugins": [
"@typescript-eslint/eslint-plugin",
"jest",
"prettier"
],
"rules": {
Expand Down
6 changes: 0 additions & 6 deletions jest.config.js

This file was deleted.

12 changes: 6 additions & 6 deletions lib/prepare.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { execa, Options } from 'execa';
import { Options } from 'execa';
import fs from 'fs';
import os from 'os';
import path from 'path';
import type { Context } from './@types/semantic-release';
import { DefaultConfig } from './default-options';
import { PluginConfig } from './types';
import { normalizeVersion, pipe, setopt } from './util';
import { normalizeVersion, pipe, setopt, spawn } from './util';
import { assertExitCode, isLegacyBuildInterface } from './verify';

async function setVersionPy(setupPy: string, version: string) {
Expand Down Expand Up @@ -34,7 +34,7 @@ async function sDistPackage(
distDir: string,
options?: Options,
) {
await execa('python3', ['-m', 'build', '--sdist', '--outdir', distDir], {
await spawn('python3', ['-m', 'build', '--sdist', '--outdir', distDir], {
...options,
cwd: srcDir,
});
Expand All @@ -46,7 +46,7 @@ async function bDistPackage(
options?: Options,
) {
try {
await execa('python3', ['-m', 'build', '--wheel', '--outdir', distDir], {
await spawn('python3', ['-m', 'build', '--wheel', '--outdir', distDir], {
...options,
cwd: srcDir,
});
Expand All @@ -56,11 +56,11 @@ async function bDistPackage(
}

async function installPackages(packages: string[], options?: Options) {
await execa('pip3', ['install', ...packages], options);
await spawn('pip3', ['install', ...packages], options);
}

async function createVenv(envDir: string, options?: Options): Promise<Options> {
await execa('python3', ['-m', 'venv', envDir], options);
await spawn('python3', ['-m', 'venv', envDir], options);
const envPath = path.resolve(envDir, 'bin');
if (os.platform() == 'win32') {
return {
Expand Down
6 changes: 3 additions & 3 deletions lib/publish.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { execa, Options, ResultPromise } from 'execa';
import { Options, ResultPromise } from 'execa';
import type { Context } from './@types/semantic-release';
import { DefaultConfig } from './default-options';
import { PluginConfig } from './types';
import { pipe } from './util.js';
import { pipe, spawn } from './util.js';

function publishPackage(
srcDir: string,
Expand All @@ -17,7 +17,7 @@ function publishPackage(
signArgs.push('--identity', gpgIdentity);
}

return execa(
return spawn(
'python3',
[
'-m',
Expand Down
29 changes: 25 additions & 4 deletions lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { execa, Options } from 'execa';
import { execa, Options, ResultPromise } from 'execa';
import path from 'path';
import { Context } from './@types/semantic-release';

async function normalizeVersion(
version: string,
options: Options = {},
): Promise<string> {
const { stdout } = await execa(
const { stdout } = await spawn(
'python3',
[
'-c',
Expand All @@ -23,7 +23,7 @@ function setopt(
option: string,
value: string,
) {
return execa(
return spawn(
'python3',
[
path.basename(setupPy),
Expand All @@ -43,4 +43,25 @@ function pipe(context: Context): Options {
};
}

export { normalizeVersion, pipe, setopt };
function spawn(
file: string | URL,
args?: readonly string[],
options?: Options,
): ResultPromise {
const cp = execa(file, args, {
...options,
stdout: undefined,
stderr: undefined,
});

if (options?.stdout) {
cp.stdout?.pipe(options.stdout, { end: false });

Check failure on line 58 in lib/util.ts

View workflow job for this annotation

GitHub Actions / test (18.x, 3.8.x)

Property 'pipe' does not exist on type 'never'.

Check failure on line 58 in lib/util.ts

View workflow job for this annotation

GitHub Actions / test (18.x, 3.11.x)

Property 'pipe' does not exist on type 'never'.

Check failure on line 58 in lib/util.ts

View workflow job for this annotation

GitHub Actions / test (20.x, 3.11.x)

Property 'pipe' does not exist on type 'never'.
}
if (options?.stderr) {
cp.stderr?.pipe(options.stderr, { end: false });

Check failure on line 61 in lib/util.ts

View workflow job for this annotation

GitHub Actions / test (18.x, 3.8.x)

Property 'pipe' does not exist on type 'never'.

Check failure on line 61 in lib/util.ts

View workflow job for this annotation

GitHub Actions / test (18.x, 3.11.x)

Property 'pipe' does not exist on type 'never'.

Check failure on line 61 in lib/util.ts

View workflow job for this annotation

GitHub Actions / test (20.x, 3.11.x)

Property 'pipe' does not exist on type 'never'.
}

return cp;
}

export { normalizeVersion, pipe, setopt, spawn };
6 changes: 3 additions & 3 deletions lib/verify.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { execa, ExecaError, Result as ExecaResult, Options } from 'execa';
import { ExecaError, Result as ExecaResult, Options } from 'execa';
import FormData from 'form-data';
import fs from 'fs';
import got from 'got';
import path from 'path';
import { Context } from './@types/semantic-release';
import { DefaultConfig } from './default-options';
import { PluginConfig } from './types';
import { pipe } from './util';
import { pipe, spawn } from './util';

function assertEnvVar(name: string) {
if (!process.env[name]) {
Expand All @@ -22,7 +22,7 @@ async function assertExitCode(
) {
let res: ExecaError | ExecaResult;
try {
res = await execa(executable, args, options);
res = await spawn(executable, args, options);
} catch (err) {
res = err as ExecaError;
}
Expand Down
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,24 @@
"private": false,
"devDependencies": {
"@semantic-release/git": "^10.0.1",
"@types/jest": "^29.5.13",
"@types/uuid": "^9.0.4",
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
"copyfiles": "^2.4.1",
"eslint": "8.50.0",
"eslint-config-prettier": "9.0.0",
"eslint-plugin-jest": "^28.8.3",
"eslint-plugin-prettier": "5.0.0",
"fs-extra": "^10.0.0",
"jest": "^29.7.0",
"prettier": "^3.0.3",
"rimraf": "^5.0.5",
"semantic-release": "^22.0.5",
"ts-jest": "^29.2.5",
"typescript": "^5.2.2",
"uuid": "^8.3.1"
"uuid": "^8.3.1",
"vitest": "^2.1.3"
},
"scripts": {
"build": "yarn clean && tsc --project tsconfig.build.json && yarn copy",
"test": "NODE_OPTIONS='$NODE_OPTIONS --experimental-vm-modules' jest",
"test": "vitest",
"lint": "eslint lib/*.ts test/*.ts",
"clean": "rimraf dist/ && rimraf .tmp/",
"copy": "copyfiles -u 1 lib/py/* dist"
Expand Down
13 changes: 11 additions & 2 deletions test/package.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { PassThrough } from 'stream';
import { expect, test } from 'vitest';
import { prepare, publish, verifyConditions } from '../lib/index';
import { genPackage, hasPackage } from './util';

class EndlessPassThrough extends PassThrough {
end() {}
close() {
super.end();
}
}

test('test semantic-release-pypi (pyproject.toml)', async () => {
if (!process.env['TESTPYPI_TOKEN']) {
console.warn(
Expand Down Expand Up @@ -68,10 +76,11 @@ build-backend = "poetry.core.masonry.api"`;
},
});

context.stdout = [new PassThrough(), 'pipe'] as any;
const stream = new EndlessPassThrough();
context.stdout = stream;

let built = false;
context.stdout?.on('data', (bytes) => {
stream.on('data', (bytes) => {
const str = bytes.toString('utf-8');
if (
str.includes('Successfully built') &&
Expand Down
8 changes: 4 additions & 4 deletions test/prepare.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { execa } from 'execa';
import { describe, expect, test } from 'vitest';
import {
bDistPackage,
createVenv,
Expand All @@ -7,7 +7,7 @@ import {
setVersionPy,
setVersionToml,
} from '../lib/prepare';
import { pipe } from '../lib/util';
import { pipe, spawn } from '../lib/util';
import { assertPackage } from '../lib/verify';
import { genPackage } from './util';

Expand Down Expand Up @@ -76,7 +76,7 @@ describe('prepare: installPackages', () => {
const pkg = 'requests';
const options = await opt();

await execa('pip3', ['uninstall', '-y', pkg], {
await spawn('pip3', ['uninstall', '-y', pkg], {
...options,
});
await expect(assertPackage(pkg, options)).rejects.toThrow();
Expand All @@ -90,6 +90,6 @@ describe('prepare: installPackages', () => {

test('prepare: createVenv', async () => {
const options = await createVenv('.tmp/.testenv-2');
const pythonPath = await execa('which', ['python3'], options);
const pythonPath = await spawn('which', ['python3'], options);
expect(pythonPath.stdout).toContain('.tmp/.testenv-2/bin/python3');
});
4 changes: 2 additions & 2 deletions test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import { rmSync } from 'fs';

export default async function (/* globalConfig: any, projectConfig: any */) {
fs.rmSync('.tmp', { recursive: true, force: true });
rmSync('.tmp', { recursive: true, force: true });
}
1 change: 1 addition & 0 deletions test/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expect, test } from 'vitest';
import { DefaultConfig } from '../lib/default-options';
import { normalizeVersion } from '../lib/util';

Expand Down
7 changes: 3 additions & 4 deletions test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import fs from 'fs';
import got from 'got';
import path from 'path';
import { v4 as uuidv4 } from 'uuid';
import { vi } from 'vitest';
import { Context } from '../lib/@types/semantic-release';
import { DefaultConfig } from '../lib/default-options';
import { PluginConfig } from '../lib/types';

class BuildInterface {
name: string;
version: string;
Expand Down Expand Up @@ -110,6 +110,7 @@ function genPluginArgs(config: PluginConfig) {
const pluginConfig: PluginConfig = {
srcDir: config.srcDir ?? `.tmp/${packageName}`,
distDir: config.distDir ?? `.tmp/${packageName}/dist`,
envDir: config.envDir ?? `.tmp/${packageName}/.venv`,
repoUrl: config.repoUrl ?? 'https://test.pypi.org/legacy/',
pypiPublish: config.pypiPublish,
};
Expand All @@ -135,7 +136,7 @@ function genPluginArgs(config: PluginConfig) {
},
/* eslint-disable @typescript-eslint/no-unused-vars */
logger: {
log: jest.fn(),
log: vi.fn(),
await: function (...message: any[]): void {
throw new Error('Function not implemented.');
},
Expand Down Expand Up @@ -185,8 +186,6 @@ function genPluginArgs(config: PluginConfig) {
throw new Error('Function not implemented.');
},
},
// stdout: process.stdout,
// stderr: process.stderr,
};

return {
Expand Down
1 change: 1 addition & 0 deletions test/verify.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, expect, test } from 'vitest';
import {
assertEnvVar,
assertExitCode,
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"allowSyntheticDefaultImports": true,
"types": [
"node",
"jest",
],
"skipLibCheck": true,
"alwaysStrict": true,
Expand All @@ -21,5 +20,6 @@
"include": [
"lib/**/*",
"test/**/*.ts",
"vitest.config.ts"
],
}
9 changes: 9 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
environment: 'node',
root: 'test',
globalSetup: './setup.ts',
},
});
Loading

0 comments on commit fed5dcd

Please sign in to comment.