Overview
ESNextTestFileCompiler and EsNextTestFileParser together handle compilation and AST-based test discovery for ES-next (modern JavaScript) test files, using Babel for transpilation and AST generation.
Description
This implementation spans two files:
ESNextTestFileCompiler (src/compiler/test-file/formats/es-next/compiler.js, 81 lines) extends APIBasedTestFileCompilerBase and uses Babel to transpile modern JavaScript (including Stage 2 proposals, Flow, and JSX) down to a format executable by Node.js. It assembles Babel options with presets (stage-2, env, react, optionally flow) and plugins (transform-runtime, module-resolver, class-properties, class-static-block, private-methods). Compiled output is cached per filename. In ESM mode, it appends a V8 optimization-disabling snippet. It supports .js, .jsx, .cjs, and (in ESM mode) .mjs extensions.
EsNextTestFileParser (src/compiler/test-file/formats/es-next/get-test-list.js, 212 lines) extends TestFileParserBase and implements AST traversal to discover fixture and test declarations in ES-next source. It uses Babel to parse code into an AST, then walks the tree recognizing CallExpression, TaggedTemplateExpression, and MemberExpression nodes that reference TestCafe's global API functions. The module exports two bound functions -- getTestList and getTestListFromCode -- used by TestCafe's IDE integration and test discovery pipeline.
Usage
The ES-next compiler is the default compiler for .js, .jsx, .cjs, and .mjs test files. TestCafe's compiler pipeline selects it based on file extension. The parser is used by IDE plugins and the test list retrieval API to enumerate fixtures and tests without executing the file.
Code Reference
Source Location: Compiler
Source Location: Parser
Signature: ESNextTestFileCompiler
export default class ESNextTestFileCompiler extends APIBasedTestFileCompilerBase {
static getBabelOptions (filename, code, { esm } = {}): object
_compileCode (code, filename): string
_getRequireCompilers (): object
get canCompileInEsm (): boolean
getSupportedExtension (): string[]
}
Signature: EsNextTestFileParser
export class EsNextTestFileParser extends TestFileParserBase {
constructor ()
static getTagStrValue (exp): string
isAsyncFn (token): boolean
getTokenType (token): string
getRValue (token): object
getStringValue (token): string | null
getFunctionBody (token): object[]
getCalleeToken (token): object
getMemberFnName (token): string
formatFnData (name, value, token, meta = [{}]): object
getKeyValue (prop): { key: string, value: string | null }
analyzeMemberExp (token): object | null
formatFnArg (arg): string | null
getFnCall (token): object | null
getTaggedTemplateExp (token): object
analyzeFnCall (token): object | null
parse (code): object[]
}
// Exported bound functions
export const getTestList: (filename: string) => Promise<object[]>
export const getTestListFromCode: (code: string) => object[]
Import
import ESNextTestFileCompiler from '../compiler/test-file/formats/es-next/compiler';
import { getTestList, getTestListFromCode } from '../compiler/test-file/formats/es-next/get-test-list';
I/O Contract
ESNextTestFileCompiler.getBabelOptions(filename, code, options)
| Input |
Type |
Description
|
| filename |
string |
Path to the source file (used for source maps)
|
| code |
string |
Source code content (inspected for Flow annotations)
|
| options.esm |
boolean |
When true, loads ESM-compatible Babel libraries
|
| Output |
Type |
Description
|
| opts |
object |
Complete Babel options object with presets, plugins, sourceMaps: 'inline', and filename
|
ESNextTestFileCompiler._compileCode(code, filename)
| Input |
Type |
Description
|
| code |
string |
Raw ES-next JavaScript source code
|
| filename |
string |
Absolute path to the source file
|
| Output |
Type |
Description
|
| compiledCode |
string |
Babel-transpiled JavaScript code (cached per filename)
|
EsNextTestFileParser.parse(code)
| Input |
Type |
Description
|
| code |
string |
ES-next JavaScript source code to parse
|
| Output |
Type |
Description
|
| testList |
object[] |
Array of fixture and test descriptor objects with properties: fnName (string), value (string), loc (object), start (number), end (number), meta (object), isSkipped (boolean)
|
Supported File Extensions
| Extension |
Supported |
ESM Required
|
| .js |
Yes |
No
|
| .jsx |
Yes |
No
|
| .cjs |
Yes |
No
|
| .mjs |
Yes |
Yes
|
Usage Examples
// Compile an ES-next test file (internal usage):
import ESNextTestFileCompiler from '../compiler/test-file/formats/es-next/compiler';
const compiler = new ESNextTestFileCompiler({ baseUrl: null, esm: false });
const code = `
import { Selector } from 'testcafe';
fixture('My Fixture')
.page('https://example.com');
test('My Test', async t => {
await t.click(Selector('#button'));
});
`;
const compiled = compiler._compileCode(code, '/tests/example.js');
// compiled is the Babel-transpiled CommonJS output
// Discover tests from source code without executing:
import { getTestListFromCode } from '../compiler/test-file/formats/es-next/get-test-list';
const code = `
fixture('Login Tests').page('https://example.com');
test('should login', async t => { /* ... */ });
test.skip('should logout', async t => { /* ... */ });
`;
const tests = getTestListFromCode(code);
// tests = [
// { fnName: 'fixture', value: 'Login Tests', loc: {...}, ... },
// { fnName: 'test', value: 'should login', loc: {...}, isSkipped: false, ... },
// { fnName: 'test', value: 'should logout', loc: {...}, isSkipped: true, ... },
// ]
Related Pages