|
| 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 { existsSync, readFileSync } 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 | + : existsSync(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