@@ -4,17 +4,50 @@ const { readFile } = require('fs')
44const { promisify } = require ( 'util' )
55
66const { load : loadYaml } = require ( 'js-yaml' )
7+ const isCi = require ( 'is-ci' )
78
8- const { getWatchTask } = require ( '../utils' )
9+ const { name } = require ( '../../package.json' )
10+ const { getWatchTask, pack } = require ( '../utils' )
911const gulpExeca = require ( '../exec' )
1012
1113const TRAVIS_CONFIG = `${ __dirname } /../../.travis.yml`
1214
13- const unit = ( ) => gulpExeca ( 'ava' )
15+ const unit = async function ( ) {
16+ // In CI, we use `pack`, but not locally since it is slow.
17+ // Also, in CI we do test coverage and send it to Coveralls.
18+ if ( ! isCi ) {
19+ return gulpExeca ( 'ava' )
20+ }
21+
22+ // When using `pack`, tested files will be inside `node_modules`
23+ // By default `nyc` ignore those, so we need to add them to `--include``
24+ // Even after this, `node_modules` are still ignored by `nyc` unless using
25+ // a negated `--exclude`
26+ await pack ( `nyc --include ${ NESTED_DIR } --exclude !${ NESTED_DIR } ava` )
27+
28+ await sendToCoveralls ( )
29+ }
1430
1531// eslint-disable-next-line fp/no-mutation
1632unit . description = 'Run unit tests'
1733
34+ const sendToCoveralls = async function ( ) {
35+ // We strip `node_modules/PACKAGE/` from test coverage reports so it looks
36+ // like source files were in the same directory (not inside `node_modules`).
37+ const covMap = await promisify ( readFile ) ( COVMAP_PATH , { encoding : 'utf-8' } )
38+ const covMapA = covMap . replace ( NESTED_DIR_REGEXP , '' )
39+
40+ await gulpExeca ( 'coveralls' , { input : covMapA } )
41+ }
42+
43+ const NESTED_DIR = `node_modules/${ name } `
44+ // The RegExp needs to account for Windows having different separators.
45+ const NESTED_DIR_REGEXP = new RegExp (
46+ `node_modules(\\/|\\\\)${ name } (\\/|\\\\)` ,
47+ 'gu' ,
48+ )
49+ const COVMAP_PATH = './coverage/lcov.info'
50+
1851// We have to use this to debug Ava test files with Chrome devtools
1952const unitwatch = getWatchTask ( { UNIT : unit } , unit )
2053
0 commit comments