Skip to content

Commit c8a2c1c

Browse files
committed
Distinguish between unit and e2e tests
1 parent 9f56933 commit c8a2c1c

File tree

19 files changed

+57
-54
lines changed

19 files changed

+57
-54
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ jobs:
5959
node-version: ${{ matrix.node }}
6060

6161
- name: Test
62-
run: pnpm exec vitest --coverage
62+
run: pnpm test:all --coverage
6363

6464
- name: Submit coverage
6565
uses: coverallsapp/github-action@master

bin/read-package-json.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import { readFileSync } from 'node:fs';
22
import { createRequire } from 'node:module';
33

4+
const require = createRequire(import.meta.url);
5+
46
/**
57
* Read the package.json file of `concurrently`
68
*/
79
export function readPackageJson(): Record<string, unknown> {
8-
let resolver;
9-
try {
10-
resolver = require.resolve;
11-
} catch {
12-
resolver = createRequire(import.meta.url).resolve;
13-
}
14-
const path = resolver('concurrently/package.json');
10+
const path = require.resolve('concurrently/package.json');
1511
const content = readFileSync(path, 'utf8');
1612
return JSON.parse(content);
1713
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
"lint": "eslint",
5050
"prepublishOnly": "safe-publish-latest && pnpm run build",
5151
"test": "vitest --project unit",
52+
"test:all": "vitest run",
53+
"test:e2e": "vitest run --project e2e",
5254
"test:smoke": "vitest run --project smoke",
5355
"prepare": "husky"
5456
},
Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import { spawn } from 'node:child_process';
22
import fs from 'node:fs';
3-
import os from 'node:os';
43
import path from 'node:path';
54
import readline from 'node:readline';
65

76
import { subscribeSpyTo } from '@hirez_io/observer-spy';
87
import { sendCtrlC, spawnWithWrapper } from 'ctrlc-wrapper';
9-
import { build } from 'esbuild';
108
import Rx from 'rxjs';
119
import { map } from 'rxjs/operators';
1210
import stringArgv from 'string-argv';
13-
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
11+
import { describe, expect, it } from 'vitest';
1412

15-
import { escapeRegExp } from '../lib/utils.js';
13+
import { escapeRegExp } from '../../lib/utils.js';
1614

1715
const isWindows = process.platform === 'win32';
1816
const createKillMessage = (prefix: string, signal: 'SIGTERM' | 'SIGINT' | string) => {
@@ -24,37 +22,13 @@ const createKillMessage = (prefix: string, signal: 'SIGTERM' | 'SIGINT' | string
2422
return new RegExp(`${escapeRegExp(prefix)} exited with code ${map[signal] ?? signal}`);
2523
};
2624

27-
let tmpDir: string;
28-
29-
beforeAll(async () => {
30-
// Build 'concurrently' and store it in a temporary directory
31-
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'concurrently-'));
32-
await build({
33-
entryPoints: [path.join(__dirname, 'index.ts')],
34-
platform: 'node',
35-
bundle: true,
36-
// it doesn't seem like esbuild is able to change a CJS module to ESM, so target CJS instead.
37-
// https://github.com/evanw/esbuild/issues/1921
38-
format: 'cjs',
39-
outfile: path.join(tmpDir, 'concurrently.cjs'),
40-
});
41-
fs.copyFileSync(path.join(__dirname, '..', 'package.json'), path.join(tmpDir, 'package.json'));
42-
}, 8000);
43-
44-
afterAll(() => {
45-
// Remove the temporary directory where 'concurrently' was stored
46-
if (tmpDir) {
47-
fs.rmSync(tmpDir, { recursive: true });
48-
}
49-
});
50-
5125
/**
5226
* Creates a child process running 'concurrently' with the given args.
5327
* Returns observables for its combined stdout + stderr output, close events, pid, and stdin stream.
5428
*/
5529
const run = (args: string, ctrlcWrapper?: boolean) => {
5630
const spawnFn = ctrlcWrapper ? spawnWithWrapper : spawn;
57-
const child = spawnFn('node', [path.join(tmpDir, 'concurrently.cjs'), ...stringArgv(args)], {
31+
const child = spawnFn('node', ['../../dist/bin/index.js', ...stringArgv(args)], {
5832
cwd: __dirname,
5933
env: {
6034
...process.env,
@@ -126,7 +100,7 @@ it('prints help when no arguments are passed', async () => {
126100
});
127101

128102
describe('has version command', () => {
129-
const pkg = fs.readFileSync(path.join(__dirname, '..', 'package.json'), 'utf-8');
103+
const pkg = fs.readFileSync(path.join(__dirname, '..', '..', 'package.json'), 'utf-8');
130104
const { version } = JSON.parse(pkg);
131105

132106
it.each(['--version', '-V', '-v'])('%s', async (arg) => {

tests/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
{
2+
"type": "module",
23
"dependencies": {
34
"concurrently": "workspace:*"
4-
},
5-
"scripts": {
6-
"test": "pnpm --workspace-root test:smoke"
75
}
86
}

tests/setup.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { exec as originalExec } from 'node:child_process';
2+
import util from 'node:util';
3+
4+
import type { TestProject, TestSpecification } from 'vitest/node';
5+
6+
const exec = util.promisify(originalExec);
7+
8+
function buildProject() {
9+
return exec('pnpm run build');
10+
}
11+
12+
function isBuildRequired(testFiles: TestSpecification[]) {
13+
for (const file of testFiles) {
14+
if (file.project.name === 'e2e' || file.project.name === 'smoke') {
15+
return true;
16+
}
17+
}
18+
return false;
19+
}
20+
21+
export default async function setup(project: TestProject) {
22+
// @ts-expect-error not typed
23+
const pattern: string[] | undefined = project.vitest.filenamePattern;
24+
25+
const testFiles = await project.vitest.getRelevantTestSpecifications(pattern);
26+
27+
if (isBuildRequired(testFiles)) {
28+
await buildProject();
29+
}
30+
31+
project.onTestsRerun(async (testFiles) => {
32+
if (isBuildRequired(testFiles)) {
33+
await buildProject();
34+
}
35+
});
36+
}

0 commit comments

Comments
 (0)