forked from istanbuljs/nyc
/
add-all-files.js
127 lines (98 loc) · 3.66 KB
/
add-all-files.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
'use strict'
const fs = require('../lib/fs-promises')
const path = require('path')
const t = require('tap')
const ap = require('any-path')
const NYC = require('../self-coverage')
const { parseArgv, resetState } = require('./helpers')
const fixtures = path.resolve(__dirname, 'fixtures')
const transpileHook = path.resolve(__dirname, 'fixtures/transpile-hook')
t.beforeEach(resetState)
t.test('outputs an empty coverage report for all files that are not excluded', async t => {
const nyc = new NYC(await parseArgv(fixtures))
await nyc.reset()
await nyc.addAllFiles()
const notLoadedPath = path.join(fixtures, './not-loaded.js')
const reports = (await nyc.coverageData()).filter(report => ap(report)[notLoadedPath])
const report = reports[0][notLoadedPath]
t.strictEqual(reports.length, 1)
t.strictEqual(report.s['0'], 0)
t.strictEqual(report.s['1'], 0)
})
t.test('outputs an empty coverage report for multiple configured extensions', async t => {
const cwd = path.resolve(fixtures, './conf-multiple-extensions')
const nyc = new NYC(await parseArgv(cwd))
await nyc.reset()
await nyc.addAllFiles()
const notLoadedPath1 = path.join(cwd, './not-loaded.es6')
const notLoadedPath2 = path.join(cwd, './not-loaded.js')
const reports = (await nyc.coverageData()).filter(report => {
const apr = ap(report)
return apr[notLoadedPath1] || apr[notLoadedPath2]
})
t.strictEqual(reports.length, 1)
const report1 = reports[0][notLoadedPath1]
t.strictEqual(report1.s['0'], 0)
t.strictEqual(report1.s['1'], 0)
const report2 = reports[0][notLoadedPath2]
t.strictEqual(report2.s['0'], 0)
t.strictEqual(report2.s['1'], 0)
})
t.test('transpiles .js files added via addAllFiles', async t => {
await fs.writeFile(
'./test/fixtures/needs-transpile.js',
'--> pork chop sandwiches <--\nconst a = 99',
'utf-8'
)
const nyc = new NYC(await parseArgv(fixtures, [
'--use-spawn-wrap=true',
'--require',
transpileHook
]))
await nyc.reset()
await nyc.addAllFiles()
const needsTranspilePath = path.join(fixtures, './needs-transpile.js')
const reports = (await nyc.coverageData()).filter(report => ap(report)[needsTranspilePath])
const report = reports[0][needsTranspilePath]
t.strictEqual(reports.length, 1)
t.strictEqual(report.s['0'], 0)
await fs.unlink(needsTranspilePath)
})
t.test('does not attempt to transpile files when they are excluded', async t => {
const notNeedTranspilePath = path.join(fixtures, './do-not-need-transpile.do-not-transpile')
await fs.writeFile(
notNeedTranspilePath,
'--> pork chop sandwiches <--\nconst a = 99',
'utf-8'
)
const nyc = new NYC(await parseArgv(fixtures, [
'--use-spawn-wrap=true',
`--require=${transpileHook}`,
'--extension=.do-not-transpile',
'--include=needs-transpile.do-not-transpile'
]))
await nyc.reset()
// If this ran against *.do-not-transpile it would throw
await nyc.addAllFiles()
await fs.unlink(notNeedTranspilePath)
})
t.test('transpiles non-.js files added via addAllFiles', async t => {
await fs.writeFile(
'./test/fixtures/needs-transpile.whatever',
'--> pork chop sandwiches <--\nconst a = 99',
'utf-8'
)
const nyc = new NYC(await parseArgv(fixtures, [
'--use-spawn-wrap=true',
`--require=${transpileHook}`,
'--extension=.whatever'
]))
await nyc.reset()
await nyc.addAllFiles()
const needsTranspilePath = path.join(fixtures, './needs-transpile.whatever')
const reports = (await nyc.coverageData()).filter(report => ap(report)[needsTranspilePath])
const report = reports[0][needsTranspilePath]
t.strictEqual(reports.length, 1)
t.strictEqual(report.s['0'], 0)
await fs.unlink(needsTranspilePath)
})