Skip to content

Commit d1f6a41

Browse files
committed
Improve tests for types
1 parent cf588c1 commit d1f6a41

File tree

7 files changed

+169
-9
lines changed

7 files changed

+169
-9
lines changed

.eslintrc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = {
3030
rules,
3131
overrides: [
3232
{
33-
files: ['*.ts', '*.cts', '*.mts'],
33+
files: ['*.ts', '*.cts', '*.mts', '*.cjs', '*.mjs'],
3434
extends: ['plugin:@typescript-eslint/recommended'],
3535
parser: '@typescript-eslint/parser',
3636
plugins: ['@typescript-eslint'],

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
/index.mjs
33

44
# test
5-
test/ts/*.js
65
/.tap
76

87
# coverage

package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@
1616
"prepublishOnly": "npm run build",
1717
"build": "babel -o legacy.js index.js && node ./scripts/build.js",
1818
"test:lint": "eslint .",
19-
"test:tsc": "tsc ./test/ts/simple.ts --lib ES6",
20-
"test:tsc:16": "tsc ./test/ts/simple.ts --lib ES6 --moduleResolution Node16 --module Node16",
21-
"test:ts": "node ./test/ts/simple.js",
19+
"test:ts": "ts-node ./test/ts/simple.ts",
20+
"test:ts:16": "ts-node --compilerOptions '{\"moduleResolution\": \"Node16\", \"module\": \"Node16\"}' ./test/ts/simple.ts",
21+
"test:cjs:16": "ts-node --compilerOptions '{\"moduleResolution\": \"Node16\", \"module\": \"Node16\"}' ./test/ts/simple.cjs",
22+
"test:mjs:16": "ts-node --compilerOptions '{\"moduleResolution\": \"Node16\", \"module\": \"Node16\"}' ./test/ts/simple.mjs",
2223
"tap": "tap --reporter classic",
2324
"test:git": "npm run tap test/git-check-ignore.test.js",
2425
"test:ignore": "npm run tap test/ignore.test.js",
2526
"test:ignore:only": "IGNORE_ONLY_IGNORES=1 npm run tap test/ignore.test.js",
2627
"test:others": "npm run tap test/others.test.js",
2728
"test:cases": "npm run tap test/*.test.js -- --coverage",
2829
"test:no-coverage": "npm run tap test/*.test.js -- --no-check-coverage",
29-
"test:only": "npm run test:lint && npm run build && npm run test:tsc && npm run test:tsc:16 && npm run test:ts && npm run test:cases",
30+
"test:only": "npm run test:lint && npm run build && npm run test:ts && npm run test:ts:16 && npm run test:cjs:16 && npm run test:mjs:16 && npm run test:cases",
3031
"test": "npm run test:only",
3132
"test:win32": "IGNORE_TEST_WIN32=1 npm run test",
3233
"report": "tap --coverage-report=html",
@@ -72,6 +73,7 @@
7273
"spawn-sync": "^2.0.0",
7374
"tap": "^16.3.9",
7475
"tmp": "0.2.3",
76+
"ts-node": "^10.9.2",
7577
"typescript": "^5.6.2"
7678
},
7779
"engines": {

test/ts/simple.cjs

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const ignore = require('../..') // eslint-disable-line @typescript-eslint/no-require-imports
2+
const {isPathValid} = require('../..') // eslint-disable-line @typescript-eslint/no-require-imports
3+
4+
const equal = (actual, expect, message) => {
5+
if (actual !== expect) {
6+
throw new Error(`${message}, expect: ${expect}, actual: ${actual}`)
7+
}
8+
}
9+
10+
const paths = ['a', 'a/b', 'foo/bar']
11+
12+
let ig = ignore()
13+
14+
ig = ig.add('*')
15+
ig = ig.add(['!*/', '!foo/bar'])
16+
17+
const filter = ig.createFilter()
18+
paths.filter(filter)
19+
const passed = filter('a')
20+
equal(passed, false, 'filters a out')
21+
22+
const filtered_paths = ig.filter(paths)
23+
const ignores = ig.ignores('a')
24+
equal(ignores, true, 'ignores a')
25+
26+
let ig2 = ignore()
27+
28+
ig2 = ig2.add('# test ig.add(Ignore)')
29+
ig2 = ig2.add(ig)
30+
31+
let ig3 = ignore()
32+
ig3 = ig3.add('*.js')
33+
34+
let ig4 = ignore()
35+
ig4 = ig4.add('*.png')
36+
37+
ig2 = ig2.add([ig3, ig4])
38+
39+
const ig5 = ignore({
40+
ignorecase: false
41+
})
42+
43+
const isValid = isPathValid('./foo')
44+
equal(isValid, false, './foo is not valid')
45+
46+
const {
47+
ignored,
48+
unignored
49+
} = ig4.test('foo')
50+
51+
equal(ignored, false, 'not ignored')
52+
equal(unignored, false, 'not unignored')
53+
54+
// Filter an Readyonly array
55+
const readonlyPaths = ['a', 'a/b', 'foo/bar']
56+
ig.filter(readonlyPaths)
57+
58+
// Add an Readonly array of rules
59+
const ig6 = ignore()
60+
ig6.add([ig3, ig4])
61+
62+
// options.ignoreCase and options.allowRelativePaths
63+
ignore({
64+
ignoreCase: false,
65+
allowRelativePaths: true
66+
})
67+
68+
const ig7 = ignore()
69+
70+
ig7.add({pattern: 'foo/*', mark: '10'})
71+
const {
72+
ignored: ignored7,
73+
rule: ignoreRule7
74+
} = ig7.checkIgnore('foo/')
75+
76+
equal(ignored7, true, 'should ignore')
77+
equal(ignoreRule7.mark, '10', 'mark is 10')
78+
equal(ignoreRule7.pattern, 'foo/*', 'ignored by foo/*')

test/ts/simple.mjs

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import ignore, {isPathValid} from '../../index.mjs' // eslint-disable-line import/extensions
2+
3+
const equal = (actual, expect, message) => {
4+
if (actual !== expect) {
5+
throw new Error(`${message}, expect: ${expect}, actual: ${actual}`)
6+
}
7+
}
8+
9+
const paths = ['a', 'a/b', 'foo/bar']
10+
11+
let ig = ignore()
12+
13+
ig = ig.add('*')
14+
ig = ig.add(['!*/', '!foo/bar'])
15+
16+
const filter = ig.createFilter()
17+
paths.filter(filter)
18+
const passed = filter('a')
19+
equal(passed, false, 'filters a out')
20+
21+
const filtered_paths = ig.filter(paths)
22+
const ignores = ig.ignores('a')
23+
equal(ignores, true, 'ignores a')
24+
25+
let ig2 = ignore()
26+
27+
ig2 = ig2.add('# test ig.add(Ignore)')
28+
ig2 = ig2.add(ig)
29+
30+
let ig3 = ignore()
31+
ig3 = ig3.add('*.js')
32+
33+
let ig4 = ignore()
34+
ig4 = ig4.add('*.png')
35+
36+
ig2 = ig2.add([ig3, ig4])
37+
38+
const ig5 = ignore({
39+
ignorecase: false
40+
})
41+
42+
const isValid = isPathValid('./foo')
43+
equal(isValid, false, './foo is not valid')
44+
45+
const {
46+
ignored,
47+
unignored
48+
} = ig4.test('foo')
49+
50+
equal(ignored, false, 'not ignored')
51+
equal(unignored, false, 'not unignored')
52+
53+
// Filter an Readyonly array
54+
const readonlyPaths = ['a', 'a/b', 'foo/bar']
55+
ig.filter(readonlyPaths)
56+
57+
// Add an Readonly array of rules
58+
const ig6 = ignore()
59+
ig6.add([ig3, ig4])
60+
61+
// options.ignoreCase and options.allowRelativePaths
62+
ignore({
63+
ignoreCase: false,
64+
allowRelativePaths: true
65+
})
66+
67+
const ig7 = ignore()
68+
69+
ig7.add({pattern: 'foo/*', mark: '10'})
70+
const {
71+
ignored: ignored7,
72+
rule: ignoreRule7
73+
} = ig7.checkIgnore('foo/')
74+
75+
equal(ignored7, true, 'should ignore')
76+
equal(ignoreRule7.mark, '10', 'mark is 10')
77+
equal(ignoreRule7.pattern, 'foo/*', 'ignored by foo/*')

test/ts/simple.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import ignore, {isPathValid} from '../..'
22
import type {Ignore} from '../..'
33

4-
const equal = (actual, expect, message) => {
4+
const equal = (actual: unknown, expect: unknown, message: string) => {
55
if (actual !== expect) {
66
throw new Error(`${message}, expect: ${expect}, actual: ${actual}`)
77
}
@@ -77,5 +77,5 @@ const {
7777
} = ig7.checkIgnore('foo/')
7878

7979
equal(ignored7, true, 'should ignore')
80-
equal(ignoreRule7.mark, '10', 'mark is 10')
81-
equal(ignoreRule7.pattern, 'foo/*', 'ignored by foo/*')
80+
equal(ignoreRule7?.mark, '10', 'mark is 10')
81+
equal(ignoreRule7?.pattern, 'foo/*', 'ignored by foo/*')

test/tsconfig.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"compilerOptions": {
3+
}
4+
}

0 commit comments

Comments
 (0)