Skip to content

Commit be620da

Browse files
committed
test constructor interface
1 parent 9f8168d commit be620da

File tree

4 files changed

+95
-9
lines changed

4 files changed

+95
-9
lines changed

.taprc

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"timeout": 10,
33
"reporter": "spec",
4-
"check-coverage": false,
5-
"no-coverage": true,
4+
65
"test-regex": "\\w+\\.test\\.js$"
76
}

index.constructor.test.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import tap from 'tap'
2+
import { fileURLToPath } from 'url'
3+
import { dirname as dirnamePath } from 'path'
4+
import CommonESM from './index.js'
5+
6+
const {
7+
__dirname,
8+
__filename,
9+
__isMain,
10+
dirname,
11+
filename,
12+
isMain,
13+
join,
14+
requireJson
15+
} = new CommonESM(import.meta.url)
16+
17+
const ___dirname = dirnamePath(fileURLToPath(import.meta.url))
18+
const ___filename = fileURLToPath(import.meta.url)
19+
20+
tap.test('__dirname', (t) => {
21+
t.equal(
22+
__dirname,
23+
___dirname,
24+
'__dirname should return the current directory'
25+
)
26+
t.end()
27+
})
28+
29+
tap.test('__filename', (t) => {
30+
t.equal(__filename, ___filename, '__filename should return the current file')
31+
t.end()
32+
})
33+
34+
tap.test('__isMain', (t) => {
35+
t.equal(__isMain, true, '__isMain should return true')
36+
t.end()
37+
})
38+
39+
tap.test('dirname()', (t) => {
40+
t.equal(dirname(), ___dirname, 'dirname should return the current directory')
41+
t.end()
42+
})
43+
44+
tap.test('filename()', (t) => {
45+
t.equal(filename(), ___filename, 'filename should return the current file')
46+
t.end()
47+
})
48+
49+
tap.test('isMain()', (t) => {
50+
t.equal(isMain(), true, 'isMain should return true')
51+
t.end()
52+
})
53+
54+
tap.test('join()', (t) => {
55+
t.equal(
56+
join('a', 'b', 'c'),
57+
`${___dirname}/a/b/c`,
58+
'join should join the given paths'
59+
)
60+
t.end()
61+
})
62+
63+
tap.test('requireJson()', (t) => {
64+
t.equal(
65+
requireJson('./package.json').name,
66+
'@uscreen.de/common-esm',
67+
'requireJson should require a JSON file'
68+
)
69+
t.end()
70+
})

index.test.js index.function.test.js

-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ import { fileURLToPath } from 'url'
33
import { dirname as dirnamePath } from 'path'
44
import { dirname, filename, join, requireJson, isMain } from './index.js'
55

6-
tap.test('Test Setup', (t) => {
7-
t.equal(true, true, 'Tests and assertions should work')
8-
t.end()
9-
})
10-
116
const __dirname = dirnamePath(fileURLToPath(import.meta.url))
127
const __filename = fileURLToPath(import.meta.url)
138

index.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { readFileSync } from 'fs'
2-
import desm, { dirname, filename, join } from 'desm'
2+
import { dirname, filename, join } from 'desm'
33

44
/**
55
* like const { name } = require('./package.json')
@@ -12,5 +12,27 @@ const requireJson = (metaUrl, file) =>
1212
*/
1313
const isMain = (metaUrl) => filename(metaUrl) === process.argv[1]
1414

15+
/**
16+
* pure function
17+
* import CommonESM = from '@uscreen.de/common-esm'
18+
* const { __dirname, __filename } = new CommonESM(import.meta.url)
19+
*/
20+
function CommonESM(metaUrl) {
21+
const __dirname = dirname(metaUrl)
22+
const __filename = filename(metaUrl)
23+
const __isMain = isMain(metaUrl)
24+
25+
return {
26+
__dirname,
27+
__filename,
28+
__isMain,
29+
dirname: () => dirname(metaUrl),
30+
filename: () => filename(metaUrl),
31+
isMain: () => isMain(metaUrl),
32+
join: (...args) => join(metaUrl, ...args),
33+
requireJson: (file) => requireJson(metaUrl, file)
34+
}
35+
}
36+
1537
export { dirname, filename, join, requireJson, isMain }
16-
export default desm
38+
export default CommonESM

0 commit comments

Comments
 (0)