import type * as d from '@stencil/core/internal' ; import { buildJestArgv , getProjectListFromCLIArgs } from './jest-config' ; import { setScreenshotEmulateData } from '../puppeteer/puppeteer-emulate' ; import type { AggregatedResult } from '@jest/test-result' ; export async function runJest ( config , env ) { let success = false ; try { const emulateConfigs = getEmulateConfigs ( config . testing , config . flags ) ; env . __STENCIL_EMULATE_CONFIGS__ = JSON . stringify ( emulateConfigs ) ; env . __STENCIL_ENV__ = JSON . stringify ( config . env ) ; if ( config . flags . ci || config . flags . e2e ) { env . __STENCIL_DEFAULT_TIMEOUT__ = '30000' ; } else { env . __STENCIL_DEFAULT_TIMEOUT__ = '15000' ; } if ( config . flags . devtools ) { env . __STENCIL_DEFAULT_TIMEOUT__ = '300000000' ; } config . logger . debug ( `default timeout: ${ env . __STENCIL_DEFAULT_TIMEOUT__ }` ) ; const jestArgv = buildJestArgv ( config ) ; const projects = getProjectListFromCLIArgs ( config , jestArgv ) ; const { runCLI } = require ( '@jest/core' ) ; const cliResults = ( await runCLI ( jestArgv , projects ) ) as { results : AggregatedResult ; } ; success = ! ! cliResults . results . success ; } catch ( e ) { config . logger . error ( `runJest: ${ e }` ) ; } return success ; } returns export function createTestRunner ( ) { const TestRunner = require ( 'jest-runner' ) . default ?? require ( 'jest-runner' ) ; class StencilTestRunner extends TestRunner { async runTests ( tests , watcher , onStart , onResult , onFailure , options ) { const env = process . env as d . E2EProcessEnv ; tests = tests . filter ( ( t ) => includeTestFile ( t . path , env ) ) ; if ( env . __STENCIL_SCREENSHOT__ === 'true' ) { const emulateConfigs = JSON . parse ( env . __STENCIL_EMULATE_CONFIGS__ ) as d . EmulateConfig [ ] ; for ( let i = 0 ; i < emulateConfigs . length ; i ++ ) { const emulateConfig = emulateConfigs [ i ] ; setScreenshotEmulateData ( emulateConfig , env ) ; await super . runTests ( tests , watcher , onStart , onResult , onFailure , options ) ; } } else { await super . runTests ( tests , watcher , onStart , onResult , onFailure , options ) ; } } } return StencilTestRunner ; } export function includeTestFile ( testPath , env ) { testPath = testPath . toLowerCase ( ) . replace ( /\\/g , '/' ) ; const hasE2E = testPath . includes ( '.e2e.' ) || testPath . includes ( '/e2e.' ) ; if ( env . __STENCIL_E2E_TESTS__ === 'true' && hasE2E ) { return true ; } if ( env . __STENCIL_SPEC_TESTS__ === 'true' && ! hasE2E ) { return true ; } return false ; } export function getEmulateConfigs ( testing , flags ) { let emulateConfigs = testing . emulate . slice ( ) ; if ( typeof flags . emulate === 'string' ) { const emulateFlag = flags . emulate . toLowerCase ( ) ; emulateConfigs = emulateConfigs . filter ( ( emulateConfig ) => { if ( typeof emulateConfig . device === 'string' && emulateConfig . device . toLowerCase ( ) === emulateFlag ) { return true ; } if ( typeof emulateConfig . userAgent === 'string' && emulateConfig . userAgent . toLowerCase ( ) . includes ( emulateFlag ) ) { return true ; } return false ; } ) ; } return emulateConfigs ; }