Skip to content

Commit a859644

Browse files
sorccuhenrist
andauthored
feat: full ESM config support using Jiti (build #1041) (#1048)
* feat: full ESM config support using Jiti * fix(ci): set fail-fast to false for the test matrix to see all results * fix: contains -> includes, contains is deprecated * chore: minor wording change * fix: run jest with --experimental-vm-modules, needed on Windows * fix: make sure we always disable ts-node if enabled * ci: temporarily disable windows testing until jest/jiti compat is worked out --------- Co-authored-by: Henrik Steen <henrist@henrist.net>
1 parent 79249e1 commit a859644

File tree

18 files changed

+239
-128
lines changed

18 files changed

+239
-128
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ jobs:
2222
- run: npm run lint
2323
test:
2424
strategy:
25+
fail-fast: false
2526
matrix:
26-
os: [ubuntu-latest, windows-latest]
27+
os: [ubuntu-latest]
2728
name: test - ${{ matrix.os }}
2829
runs-on: ${{ matrix.os }}
2930
steps:

examples/advanced-project/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"license": "ISC",
1212
"devDependencies": {
1313
"checkly": "latest",
14-
"ts-node": "10.9.1",
15-
"typescript": "4.9.5"
14+
"jiti": "^2"
1615
}
1716
}

examples/boilerplate-project/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"license": "ISC",
1212
"devDependencies": {
1313
"checkly": "latest",
14-
"ts-node": "10.9.1",
15-
"typescript": "4.9.5"
14+
"jiti": "^2"
1615
}
1716
}

package-lock.json

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cli/package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"clean": "rimraf ./dist",
1616
"prepack": "npx oclif manifest",
1717
"prepare": "npm run clean && tsc --build",
18-
"test": "jest --selectProjects unit",
19-
"test:e2e": "npm run prepare && cross-env NODE_CONFIG_DIR=./e2e/config jest --selectProjects E2E",
18+
"test": "cross-env NODE_OPTIONS=\"--experimental-vm-modules\" jest --selectProjects unit",
19+
"test:e2e": "npm run prepare && cross-env NODE_OPTIONS=\"--experimental-vm-modules\" NODE_CONFIG_DIR=./e2e/config jest --selectProjects E2E",
2020
"test:e2e:local": "cross-env CHECKLY_BASE_URL=http://localhost:3000 CHECKLY_ENV=local npm run test:e2e",
2121
"watch": "tsc --watch"
2222
},
@@ -112,6 +112,7 @@
112112
"config": "^3.3.12",
113113
"cross-env": "^7.0.3",
114114
"jest": "^29.7.0",
115+
"jiti": "^2.4.2",
115116
"nanoid": "^3.3.11",
116117
"oclif": "^4.17.44",
117118
"rimraf": "^5.0.10",
@@ -120,6 +121,14 @@
120121
"ts-node": "^10.9.2",
121122
"typescript": "^5.3.3"
122123
},
124+
"peerDependencies": {
125+
"jiti": ">=2"
126+
},
127+
"peerDependenciesMeta": {
128+
"jiti": {
129+
"optional": true
130+
}
131+
},
123132
"jest": {
124133
"testTimeout": 30000,
125134
"projects": [
Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1-
import path from 'path'
2-
import { loadFile } from '../services/checkly-config-loader'
31
import fs from 'fs'
2+
import path from 'path'
3+
import { loadFile } from '../services/util'
44

55
export async function loadPlaywrightConfig () {
6-
let config
7-
const filenames = ['playwright.config.ts', 'playwright.config.js']
6+
const filenames = [
7+
'playwright.config.ts',
8+
'playwright.config.mts',
9+
'playwright.config.cts',
10+
'playwright.config.js',
11+
'playwright.config.mjs',
12+
'playwright.config.cjs',
13+
]
814
for (const configFile of filenames) {
9-
if (!fs.existsSync(path.resolve(path.dirname(configFile)))) {
15+
const configPath = path.resolve(configFile)
16+
if (!fs.existsSync(configPath)) {
1017
continue
1118
}
12-
const dir = path.resolve(path.dirname(configFile))
13-
config = await loadFile(path.join(dir, configFile))
14-
if (config) {
15-
break
16-
}
19+
const result = await loadFile(configPath)
20+
return result
1721
}
18-
return config
22+
return undefined
1923
}

packages/cli/src/services/__tests__/checkly-config-loader.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('loadChecklyConfig()', () => {
2323
await loadChecklyConfig(configDir)
2424
} catch (e: any) {
2525
expect(e.message).toContain(`Unable to locate a config at ${configDir} with ${
26-
['checkly.config.ts', 'checkly.config.js', 'checkly.config.mjs'].join(', ')}.`)
26+
['checkly.config.ts', 'checkly.config.mts', 'checkly.config.cts', 'checkly.config.js', 'checkly.config.mjs', 'checkly.config.cjs'].join(', ')}.`)
2727
}
2828
})
2929
it('config TS file should export an object', async () => {

packages/cli/src/services/checkly-config-loader.ts

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from 'path'
22
import { existsSync } from 'fs'
3-
import { loadJsFile, loadTsFile } from './util'
3+
import { loadFile } from './util'
44
import { CheckProps } from '../constructs/check'
55
import { Session } from '../constructs'
66
import { Construct } from '../constructs/construct'
@@ -73,35 +73,19 @@ export type ChecklyConfig = {
7373
}
7474
}
7575

76-
// eslint-disable-next-line no-restricted-syntax
77-
enum Extension {
78-
JS = '.js',
79-
MJS = '.mjs',
80-
TS = '.ts',
81-
}
82-
83-
export function loadFile (file: string) {
84-
if (!existsSync(file)) {
85-
return Promise.resolve(null)
86-
}
87-
switch (path.extname(file)) {
88-
case Extension.JS:
89-
return loadJsFile(file)
90-
case Extension.MJS:
91-
return loadJsFile(file)
92-
case Extension.TS:
93-
return loadTsFile(file)
94-
default:
95-
throw new Error(`Unsupported file extension ${file} for the config file`)
96-
}
97-
}
98-
9976
function isString (obj: any) {
10077
return (Object.prototype.toString.call(obj) === '[object String]')
10178
}
10279

10380
export function getChecklyConfigFile (): {checklyConfig: string, fileName: string} | undefined {
104-
const filenames: string[] = ['checkly.config.ts', 'checkly.config.js', 'checkly.config.mjs']
81+
const filenames = [
82+
'checkly.config.ts',
83+
'checkly.config.mts',
84+
'checkly.config.cts',
85+
'checkly.config.js',
86+
'checkly.config.mjs',
87+
'checkly.config.cjs',
88+
]
10589
let config
10690
for (const configFile of filenames) {
10791
const dir = path.resolve(path.dirname(configFile))
@@ -120,13 +104,14 @@ export function getChecklyConfigFile (): {checklyConfig: string, fileName: strin
120104
return config
121105
}
122106

123-
export async function loadChecklyConfig (dir: string, filenames = ['checkly.config.ts', 'checkly.config.js', 'checkly.config.mjs']): Promise<{ config: ChecklyConfig, constructs: Construct[] }> {
107+
export async function loadChecklyConfig (dir: string, filenames = ['checkly.config.ts', 'checkly.config.mts', 'checkly.config.cts', 'checkly.config.js', 'checkly.config.mjs', 'checkly.config.cjs']): Promise<{ config: ChecklyConfig, constructs: Construct[] }> {
124108
let config
125109
Session.loadingChecklyConfigFile = true
126110
Session.checklyConfigFileConstructs = []
127111
for (const filename of filenames) {
128-
config = await loadFile(path.join(dir, filename))
129-
if (config) {
112+
const filePath = path.join(dir, filename)
113+
if (existsSync(filePath)) {
114+
config = await loadFile(filePath)
130115
break
131116
}
132117
}

packages/cli/src/services/project-parser.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as path from 'path'
2-
import { findFilesWithPattern, loadJsFile, loadTsFile, pathToPosix } from './util'
2+
import { findFilesWithPattern, loadFile, pathToPosix } from './util'
33
import {
44
Check, BrowserCheck, CheckGroup, Project, Session,
55
PrivateLocation, PrivateLocationCheckAssignment, PrivateLocationGroupAssignment, MultiStepCheck,
@@ -84,15 +84,11 @@ async function loadAllCheckFiles (
8484
// setting the checkFilePath is used for filtering by file name on the command line
8585
Session.checkFileAbsolutePath = checkFile
8686
Session.checkFilePath = pathToPosix(path.relative(directory, checkFile))
87-
if (checkFile.endsWith('.js')) {
88-
await loadJsFile(checkFile)
89-
} else if (checkFile.endsWith('.mjs')) {
90-
await loadJsFile(checkFile)
91-
} else if (checkFile.endsWith('.ts')) {
92-
await loadTsFile(checkFile)
87+
if (/\.[mc]?(js|ts)$/.test(checkFile)) {
88+
await loadFile(checkFile)
9389
} else {
9490
throw new Error('Unable to load check configuration file with unsupported extension. ' +
95-
`Please use a .js, .msj or .ts file instead.\n${checkFile}`)
91+
`Please use a .js, .mjs, .cjs, .ts, .mts or .cts file instead.\n${checkFile}`)
9692
}
9793
Session.checkFilePath = undefined
9894
Session.checkFileAbsolutePath = undefined

0 commit comments

Comments
 (0)