1
|
|
|
/* eslint-env es6, node */ |
2
|
|
|
/* eslint complexity: ['error', { max: 10 }] */ // set maximum cyclomatic complexity to 10; ref: <https://eslint.org/docs/rules/complexity> |
3
|
|
|
// # spell-checker:ignore (modules) Deno ESM ESMs vNodeJSMajor vNodeJSminor cyclomatic execa |
4
|
|
|
'use strict'; |
5
|
|
|
|
6
|
|
|
const fs = require('fs'); |
7
|
|
|
const path = require('path'); |
8
|
|
|
|
9
|
|
|
const test = require('ava'); |
10
|
|
|
const spawn = require('cross-spawn'); |
11
|
|
|
|
12
|
|
|
const module_ = require('../build/cjs+tests'); |
13
|
|
|
|
14
|
|
|
const vNodeJS = process.versions.node.split('.'); |
15
|
|
|
const vNodeJSMajor = +vNodeJS[0]; |
16
|
|
|
const vNodeJSminor = +vNodeJS[1]; |
17
|
|
|
|
18
|
|
|
// removal of `--experimental-modules` flag gate for ESM |
19
|
|
|
// ref: [NodeJS-v12.17 changes]<https://github.com/nodejs/node/pull/33197> |
20
|
|
|
// ref: [NodeJS-v13.2 changes]<https://github.com/nodejs/node/pull/30547> |
21
|
|
|
const settledSupportForESMs = |
22
|
|
|
vNodeJSMajor > 13 || |
23
|
|
|
(vNodeJSMajor === 13 && vNodeJSminor >= 2) || |
24
|
|
|
(vNodeJSMajor === 12 && vNodeJSminor >= 17); |
25
|
|
|
|
26
|
|
|
// Integration tests |
27
|
|
|
|
28
|
|
|
test('api', (t) => { |
29
|
|
|
const api = ['home', 'temp']; |
30
|
|
|
|
31
|
|
|
t.is(typeof module_, 'function'); |
32
|
|
|
t.deepEqual(Object.keys(module_).sort(), api.sort()); |
33
|
|
|
api.forEach((key) => { |
34
|
|
|
// eslint-disable-next-line security/detect-object-injection |
35
|
|
|
t.is(typeof module_[key], 'function'); |
36
|
|
|
}); |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
// ToDO: add Deno example script checks |
40
|
|
|
|
41
|
|
|
test('examples are executable without error (JavaScript)', (t) => { |
42
|
|
|
const egDirPath = 'eg'; |
43
|
|
|
const extensions = ['.js', '.cjs', '.mjs']; |
44
|
|
|
|
45
|
|
|
// eslint-disable-next-line security/detect-non-literal-fs-filename |
46
|
|
|
const files = fs.readdirSync(egDirPath); |
47
|
|
|
|
48
|
|
|
files |
49
|
|
|
.filter((file) => { |
50
|
|
|
return extensions.includes(path.extname(file)); |
51
|
|
|
}) |
52
|
|
|
.forEach((file) => { |
53
|
|
|
if (settledSupportForESMs || path.extname(file) === '.js') { |
54
|
|
|
const command = 'node'; |
55
|
|
|
const script = path.join(egDirPath, file); |
56
|
|
|
const args = [script]; |
57
|
|
|
const options = { shell: true, encoding: 'utf-8' }; |
58
|
|
|
|
59
|
|
|
t.log({ script }); |
60
|
|
|
|
61
|
|
|
const { error, status, stdout } = spawn.sync(command, args, options); |
62
|
|
|
|
63
|
|
|
t.log({ error, status, stdout }); |
64
|
|
|
|
65
|
|
|
t.deepEqual({ error, status }, { error: null, status: 0 }); |
66
|
|
|
} |
67
|
|
|
}); |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
test('examples are executable without error (TypeScript)', (t) => { |
71
|
|
|
const egDirPath = 'eg'; |
72
|
|
|
const extensions = ['.ts']; |
73
|
|
|
|
74
|
|
|
// eslint-disable-next-line security/detect-non-literal-fs-filename |
75
|
|
|
const files = fs.readdirSync(egDirPath); |
76
|
|
|
|
77
|
|
|
files |
78
|
|
|
.filter((file) => { |
79
|
|
|
return extensions.includes(path.extname(file)); |
80
|
|
|
}) |
81
|
|
|
.forEach((file) => { |
82
|
|
|
const command = 'node'; |
83
|
|
|
const script = path.join(egDirPath, file); |
84
|
|
|
const args = ['node_modules/ts-node/dist/bin.js', script]; |
85
|
|
|
const options = { shell: true, encoding: 'utf8' }; |
86
|
|
|
|
87
|
|
|
t.log({ script }); |
88
|
|
|
|
89
|
|
|
const { error, status, stdout } = spawn.sync(command, args, options); |
90
|
|
|
|
91
|
|
|
t.log({ error, status, stdout }); |
92
|
|
|
|
93
|
|
|
t.deepEqual({ error, status }, { error: null, status: 0 }); |
94
|
|
|
}); |
95
|
|
|
}); |
96
|
|
|
|