Skip to content

Commit 66f6cb4

Browse files
Move test-runner.mjs to exercise root and add commentary (#1525)
* Move test-runner.mjs to exercise root and add commentary * Fix for impersonate * Make executable
1 parent 2ca3200 commit 66f6cb4

File tree

302 files changed

+11207
-5505
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

302 files changed

+11207
-5505
lines changed

common/.meta/test-runner.mjs

-54
This file was deleted.

common/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"typescript-eslint": "^7.18.0"
3232
},
3333
"scripts": {
34-
"test": "corepack yarn node .meta/test-runner.mjs",
34+
"test": "corepack yarn node test-runner.mjs",
3535
"test:types": "corepack yarn tstyche",
3636
"test:implementation": "corepack yarn jest --no-cache --passWithNoTests",
3737
"lint": "corepack yarn lint:types && corepack yarn lint:ci",

common/test-runner.mjs

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* 👋🏽 Hello there reader,
5+
*
6+
* It looks like you are working on this solution using the Exercism CLI and
7+
* not the online editor. That's great! The file you are looking at executes
8+
* the various steps the online test-runner also takes.
9+
*
10+
* @see https://github.com/exercism/typescript-test-runner
11+
*
12+
* TypeScript track exercises generally consist of at least two out of three
13+
* types of tests to run.
14+
*
15+
* 1. tsc, the TypeScript compiler. This tests if the TypeScript code is valid
16+
* 2. tstyche, static analysis tests to see if the types used are expected
17+
* 3. jest, runtime implementation tests to see if the solution is correct
18+
*
19+
* If one of these three fails, this script terminates with -1, -2, or -3
20+
* respectively. If it succeeds, it terminates with exit code 0.
21+
*
22+
* @note you need corepack (bundled with node LTS) enabled in order for this
23+
* test runner to work as expected. Follow the installation and test
24+
* instructions if you see errors about corepack or pnp.
25+
*/
26+
27+
import { execSync } from 'node:child_process'
28+
import { readFileSync, existsSync } from 'node:fs'
29+
import { exit } from 'node:process'
30+
import { URL } from 'node:url'
31+
32+
/**
33+
* Before executing any tests, the test runner attempts to find the
34+
* exercise config.json file which has metadata about which types of tests
35+
* to run for this solution.
36+
*/
37+
const metaDirectory = new URL('./.meta/', import.meta.url)
38+
const exercismDirectory = new URL('./.exercism/', import.meta.url)
39+
const configDirectory = existsSync(metaDirectory)
40+
? metaDirectory
41+
: execSync(exercismDirectory)
42+
? exercismDirectory
43+
: null
44+
45+
if (configDirectory === null) {
46+
throw new Error(
47+
'Expected .meta or .exercism directory to exist, but I cannot find it.'
48+
)
49+
}
50+
51+
const configFile = new URL('./config.json', configDirectory)
52+
if (!existsSync(configFile)) {
53+
throw new Error('Expected config.json to exist at ' + configFile.toString())
54+
}
55+
56+
// Experimental: import config from './config.json' with { type: 'json' }
57+
/** @type {import('./config.json') } */
58+
const config = JSON.parse(readFileSync(configFile))
59+
60+
const jest = !config.custom || config.custom['flag.tests.jest']
61+
const tstyche = config.custom?.['flag.tests.tstyche']
62+
console.log(
63+
`[tests] tsc: ✅, tstyche: ${tstyche ? '✅' : '❌'}, jest: ${jest ? '✅' : '❌'}, `
64+
)
65+
66+
/**
67+
* 1. tsc: the typescript compiler
68+
*/
69+
try {
70+
console.log('[tests] tsc (compile)')
71+
execSync('corepack yarn lint:types', {
72+
stdio: 'inherit',
73+
cwd: process.cwd(),
74+
})
75+
} catch {
76+
exit(-1)
77+
}
78+
79+
/**
80+
* 2. tstyche: type tests
81+
*/
82+
if (tstyche) {
83+
try {
84+
console.log('[tests] tstyche (type tests)')
85+
execSync('corepack yarn test:types', {
86+
stdio: 'inherit',
87+
cwd: process.cwd(),
88+
})
89+
} catch {
90+
exit(-2)
91+
}
92+
}
93+
94+
/**
95+
* 3. jest: implementation tests
96+
*/
97+
if (jest) {
98+
try {
99+
console.log('[tests] tstyche (implementation tests)')
100+
execSync('corepack yarn test:implementation', {
101+
stdio: 'inherit',
102+
cwd: process.cwd(),
103+
})
104+
} catch {
105+
exit(-3)
106+
}
107+
}
108+
109+
/**
110+
* Done! 🥳
111+
*/

exercises/concept/lasagna/.meta/test-runner.mjs

-54
This file was deleted.

exercises/concept/lasagna/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"typescript-eslint": "^7.18.0"
2929
},
3030
"scripts": {
31-
"test": "corepack yarn node .meta/test-runner.mjs",
31+
"test": "corepack yarn node test-runner.mjs",
3232
"test:types": "corepack yarn tstyche",
3333
"test:implementation": "corepack yarn jest --no-cache --passWithNoTests",
3434
"lint": "corepack yarn lint:types && corepack yarn lint:ci",
+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* 👋🏽 Hello there reader,
5+
*
6+
* It looks like you are working on this solution using the Exercism CLI and
7+
* not the online editor. That's great! The file you are looking at executes
8+
* the various steps the online test-runner also takes.
9+
*
10+
* @see https://github.com/exercism/typescript-test-runner
11+
*
12+
* TypeScript track exercises generally consist of at least two out of three
13+
* types of tests to run.
14+
*
15+
* 1. tsc, the TypeScript compiler. This tests if the TypeScript code is valid
16+
* 2. tstyche, static analysis tests to see if the types used are expected
17+
* 3. jest, runtime implementation tests to see if the solution is correct
18+
*
19+
* If one of these three fails, this script terminates with -1, -2, or -3
20+
* respectively. If it succeeds, it terminates with exit code 0.
21+
*
22+
* @note you need corepack (bundled with node LTS) enabled in order for this
23+
* test runner to work as expected. Follow the installation and test
24+
* instructions if you see errors about corepack or pnp.
25+
*/
26+
27+
import { execSync } from 'node:child_process'
28+
import { readFileSync, existsSync } from 'node:fs'
29+
import { exit } from 'node:process'
30+
import { URL } from 'node:url'
31+
32+
/**
33+
* Before executing any tests, the test runner attempts to find the
34+
* exercise config.json file which has metadata about which types of tests
35+
* to run for this solution.
36+
*/
37+
const metaDirectory = new URL('./.meta/', import.meta.url)
38+
const exercismDirectory = new URL('./.exercism/', import.meta.url)
39+
const configDirectory = existsSync(metaDirectory)
40+
? metaDirectory
41+
: execSync(exercismDirectory)
42+
? exercismDirectory
43+
: null
44+
45+
if (configDirectory === null) {
46+
throw new Error(
47+
'Expected .meta or .exercism directory to exist, but I cannot find it.'
48+
)
49+
}
50+
51+
const configFile = new URL('./config.json', configDirectory)
52+
if (!existsSync(configFile)) {
53+
throw new Error('Expected config.json to exist at ' + configFile.toString())
54+
}
55+
56+
// Experimental: import config from './config.json' with { type: 'json' }
57+
/** @type {import('./config.json') } */
58+
const config = JSON.parse(readFileSync(configFile))
59+
60+
const jest = !config.custom || config.custom['flag.tests.jest']
61+
const tstyche = config.custom?.['flag.tests.tstyche']
62+
console.log(
63+
`[tests] tsc: ✅, tstyche: ${tstyche ? '✅' : '❌'}, jest: ${jest ? '✅' : '❌'}, `
64+
)
65+
66+
/**
67+
* 1. tsc: the typescript compiler
68+
*/
69+
try {
70+
console.log('[tests] tsc (compile)')
71+
execSync('corepack yarn lint:types', {
72+
stdio: 'inherit',
73+
cwd: process.cwd(),
74+
})
75+
} catch {
76+
exit(-1)
77+
}
78+
79+
/**
80+
* 2. tstyche: type tests
81+
*/
82+
if (tstyche) {
83+
try {
84+
console.log('[tests] tstyche (type tests)')
85+
execSync('corepack yarn test:types', {
86+
stdio: 'inherit',
87+
cwd: process.cwd(),
88+
})
89+
} catch {
90+
exit(-2)
91+
}
92+
}
93+
94+
/**
95+
* 3. jest: implementation tests
96+
*/
97+
if (jest) {
98+
try {
99+
console.log('[tests] tstyche (implementation tests)')
100+
execSync('corepack yarn test:implementation', {
101+
stdio: 'inherit',
102+
cwd: process.cwd(),
103+
})
104+
} catch {
105+
exit(-3)
106+
}
107+
}
108+
109+
/**
110+
* Done! 🥳
111+
*/

0 commit comments

Comments
 (0)