swc/crates/swc_estree_compat/tests
Donny/강동윤 5520b236dd
refactor(es/parser): Rename EsConfig and TsConfig (#9094)
**Description:**

`EsConfig` and `TsConfig` sound like a general configuration for the whole language, while actually it's only about parsing.

To avoid a breaking change, I created type aliases that will work without changing the code, while warning the users.

**Related issue:**

 - Closes #9089.
2024-06-22 01:56:37 +09:00
..
fixtures refactor(es/estree): Rename: babel => estree (#2846) 2021-11-24 05:34:41 +09:00
flavor/acorn feat(es/ast): Add raw to Str (#4071) 2022-03-22 07:54:08 +00:00
babelgen.js refactor(es/estree): Rename: babel => estree (#2846) 2021-11-24 05:34:41 +09:00
compare.sh refactor(es/estree): Rename: babel => estree (#2846) 2021-11-24 05:34:41 +09:00
convert.rs refactor(es/parser): Rename EsConfig and TsConfig (#9094) 2024-06-22 01:56:37 +09:00
flavor.rs build(cargo): Update rustc to nightly-2023-03-20 (#7170) 2023-03-30 08:06:02 +00:00
package.json chore(repo): Configure prettier (#4523) 2022-05-04 14:25:28 +00:00
README.md refactor(es/estree): Rename: babel => estree (#2846) 2021-11-24 05:34:41 +09:00
swcgen.js refactor(es/estree): Rename: babel => estree (#2846) 2021-11-24 05:34:41 +09:00

How tests work

The babel-compat tests are mostly written as fixtures, similar to the @babel/parser tests. The src/convert.rs test runner looks in the fixtures/ directory for input and expected output files. Input files are parsed into an SWC AST and converted to a Babel AST in Rust. Output files are parsed directly into a Babel AST. The two ASTs are then compared, with any differences causing the test to fail.

How to write a test

Step 1: Create a new fixture dir and input file.

mkdir fixtures/my-test
echo "var a = true;" > fixtures/my-test/input.js

Step 2: Generate an output file with the expected Babel AST as JSON. There's a utility script available to help, but you can do this however you like.

# If using the babelgen.js utility, run `npm install` first to get @babel/parser dependency.
node babelgen.js fixtures/my-test/input.js > fixtures/my-test/output.json

Step 3: cargo test should now pick up your new test automatically.

There's a small, insignificant different between the default Babel AST and the converted one causing my test to fail.

This happens a lot with None and Some(false). You'll probably want to add a normalizer function to the Normalizer visitor in src/normalize/mod.rs.

Other random utlities

  • swcgen.js: Prints the SWC AST as JSON.
  • compare.sh: prints the Babel and SWC ASTs side-by-side.