1+ /*
2+ eslint-disable
3+ jest/no-identical-title,
4+ jest/no-disabled-tests,
5+ jest/expect-expect,
6+ jest/no-export,
7+ jest/no-jest-import,
8+ @typescript -eslint/explicit-module-boundary-types
9+ */
10+
111// All other imports are lazy so that single tasks start up fast.
212import gulp from 'gulp' ;
313import { promises } from 'fs' ;
414
5- const ALL_MODULES = [
6- 'tinybase' ,
7- 'ui-react' ,
15+ const TEST_MODULES = [ 'tinybase' , 'ui-react' ] ;
16+ const ALL_MODULES = TEST_MODULES . concat ( [
817 'store' ,
918 'checkpoints' ,
1019 'indexes' ,
1120 'metrics' ,
1221 'relationships' ,
1322 'persisters' ,
1423 'common' ,
15- ] ;
24+ ] ) ;
1625const LIB_DIR = 'lib' ;
26+ const TMP_DIR = './tmp' ;
1727
1828const getPrettierConfig = async ( ) => ( {
1929 ...JSON . parse ( await promises . readFile ( '.prettierrc' , 'utf-8' ) ) ,
2030 parser : 'typescript' ,
2131} ) ;
2232
2333const allOf = async ( array , cb ) => await Promise . all ( array . map ( cb ) ) ;
34+ const testModules = async ( cb ) => await allOf ( TEST_MODULES , cb ) ;
2435const allModules = async ( cb ) => await allOf ( ALL_MODULES , cb ) ;
2536
2637const makeDir = async ( dir ) => {
@@ -197,6 +208,61 @@ const compileModule = async (module, debug, dir = LIB_DIR, format = 'es') => {
197208 await ( await rollup ( inputConfig ) ) . write ( outputConfig ) ;
198209} ;
199210
211+ const test = async ( dir , { coverage, countAsserts, puppeteer, serialTests} ) => {
212+ const { default : jest } = await import ( 'jest' ) ;
213+ await makeDir ( TMP_DIR ) ;
214+ const {
215+ results : { success} ,
216+ } = await jest . runCLI (
217+ {
218+ roots : [ dir ] ,
219+ ...( puppeteer
220+ ? {
221+ setupFilesAfterEnv : [ 'expect-puppeteer' ] ,
222+ preset : 'jest-puppeteer' ,
223+ detectOpenHandles : true ,
224+ }
225+ : { testEnvironment : 'jsdom' } ) ,
226+ ...( coverage
227+ ? {
228+ collectCoverage : true ,
229+ coverageProvider : 'babel' ,
230+ collectCoverageFrom : [ `${ LIB_DIR } /debug/tinybase.js` ] ,
231+ coverageReporters : [ 'text-summary' , 'json-summary' ] ,
232+ coverageDirectory : 'tmp' ,
233+ }
234+ : { } ) ,
235+ ...( countAsserts
236+ ? {
237+ setupFilesAfterEnv : [ './test/jest/setup' ] ,
238+ reporters : [ 'default' , './test/jest/reporter' ] ,
239+ testEnvironment : './test/jest/environment' ,
240+ runInBand : true ,
241+ }
242+ : { } ) ,
243+ ...( serialTests ? { runInBand : true } : { } ) ,
244+ } ,
245+ [ '' ] ,
246+ ) ;
247+ if ( ! success ) {
248+ throw 'Test failed' ;
249+ }
250+ if ( coverage ) {
251+ await promises . writeFile (
252+ 'coverage.json' ,
253+ JSON . stringify ( {
254+ ...( countAsserts
255+ ? JSON . parse ( await promises . readFile ( './tmp/assertion-summary.json' ) )
256+ : { } ) ,
257+ ...JSON . parse ( await promises . readFile ( './tmp/coverage-summary.json' ) )
258+ . total ,
259+ } ) ,
260+ 'utf-8' ,
261+ ) ;
262+ }
263+ await removeDir ( TMP_DIR ) ;
264+ } ;
265+
200266const npmInstall = async ( ) => {
201267 const { exec} = await import ( 'child_process' ) ;
202268 const { promisify} = await import ( 'util' ) ;
@@ -216,14 +282,23 @@ export const lint = async () => await lintCheck('.');
216282export const spell = async ( ) => {
217283 await spellCheck ( '.' ) ;
218284 await spellCheck ( 'src' , true ) ;
285+ await spellCheck ( 'test' , true ) ;
219286} ;
220287
221288export const ts = async ( ) => {
222289 await copyDefinitions ( ) ;
223290 await tsCheck ( 'src' ) ;
224291 await copyDefinitions ( `${ LIB_DIR } /debug` ) ;
292+ await tsCheck ( 'test' ) ;
225293} ;
226294
295+ export const compileForTest = async ( ) => {
296+ await clearDir ( LIB_DIR ) ;
297+ await testModules ( async ( module ) => {
298+ await compileModule ( module , true , `${ LIB_DIR } /debug` ) ;
299+ } ) ;
300+ await copyDefinitions ( `${ LIB_DIR } /debug` ) ;
301+ } ;
227302export const compileForProd = async ( ) => {
228303 await clearDir ( LIB_DIR ) ;
229304 await allModules ( async ( module ) => {
@@ -235,13 +310,36 @@ export const compileForProd = async () => {
235310 await copyDefinitions ( `${ LIB_DIR } /debug` ) ;
236311} ;
237312
238- export const preCommit = gulp . series ( lint , spell , ts , compileForProd ) ;
313+ export const testUnit = async ( ) => {
314+ await test ( 'test/unit' , { coverage : true } ) ;
315+ } ;
316+ export const testUnitCountAsserts = async ( ) => {
317+ await test ( 'test/unit' , { coverage : true , countAsserts : true } ) ;
318+ } ;
319+ export const compileAndTestUnit = gulp . series ( compileForTest , testUnit ) ;
320+
321+ export const testPerf = async ( ) => {
322+ await test ( 'test/perf' , { serialTests : true } ) ;
323+ } ;
324+ export const compileAndTestPerf = gulp . series ( compileForTest , testPerf ) ;
325+
326+ export const preCommit = gulp . series (
327+ lint ,
328+ spell ,
329+ ts ,
330+ compileForTest ,
331+ testUnit ,
332+ compileForProd ,
333+ ) ;
239334
240335export const prePublishPackage = gulp . series (
241336 npmInstall ,
242337 lint ,
243338 spell ,
244339 ts ,
340+ compileForTest ,
341+ testUnitCountAsserts ,
342+ testPerf ,
245343 compileForProd ,
246344) ;
247345
0 commit comments