Skip to content

Commit

Permalink
Add a script to run tests in isolated manner from JavaScript debug te…
Browse files Browse the repository at this point in the history
…rminal
  • Loading branch information
MangelMaxime committed Mar 26, 2024
1 parent bcc8acb commit 90c2659
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 6 deletions.
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ You can see the available options by running:
./build.sh --help
```

When using the test command, you can focus on a specific by forwarding arguments to the `ava`:
When using the test command, you can focus on a specific by forwarding arguments to `vitest`:

```bash
# Focus all tests containing "class"
./build.sh test --watch -- --match="**class**"
# Focus all tests containing related to specs/class/
./build.sh test --watch -- -t class/
```

If you need to run a local version of `@glutinum/cli`, you can use `./build.sh cli [--watch]` and then run `node cli.js <args>`.
Expand Down Expand Up @@ -73,25 +73,27 @@ type FSharpAttribute =

### Tests

#### Vitest

This project use [Vitest](https://vitest.dev/) for running tests.

Vitest was chosen because it seems to have a lot of attention and is actively maintained. Plus, it seems well integrated with VSCode and Rider, allowing us to use the test explorer and even debug the tests using source maps.

If you prefer, it is possible to run the tests via the CLI.

#### VSCode
##### VSCode

Install [Vitest plugin](https://marketplace.visualstudio.com/items?itemName=vitest.explorer), then you will be able to run the tests from the test explorer.

#### Rider
##### Rider

You need to add a new configuration of type `Vitest`.

1. `Run > Edit Configurations...`
2. Add a new configuration of type `Vitest`
3. Then you can run the tests by selecting the configuration in the top right corner of the IDE.

#### Specs
##### Specs

> [!TIP]
> Run `./build.sh --help` to see the available options (look for `test specs` command).
Expand Down Expand Up @@ -150,6 +152,16 @@ The `.fsx` correspond to the expected result suffixed with the following:

> This allows us to have IDE support in the `.fsx` file instead of having a lot of syntax errors.
#### `tests/specs/index.js`

Sometimes debugging tests through Vitest runner / extensions, is not easy. This is why, we provide a `./tests/index.js` scripts which allows you to manually check a specs transformation.

```bash
node --enable-source-maps tests/specs tests/specs/references/exports/variable.d.ts
```

This avoid situation where Vitest extension needs a restart of VSCode to work again, etc.

## Tools

One handy tool when working on the TypeScript AST is [TypeScript AST Viewer](https://ts-ast-viewer.com/).
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
"@ts-morph/bootstrap": "^0.22.0",
"@vitejs/plugin-react": "^4.2.1",
"@vitest/ui": "^1.4.0",
"ansi-colors": "^4.1.3",
"bulma": "^0.9.4",
"diff": "^5.2.0",
"dirname-filename-esm": "^1.1.1",
"fable-css-modules": "^1.7.0",
"gh-pages": "^6.1.1",
Expand Down
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions tests/specs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//------------------------------------------------------------------------------
//
// This file is used to manually test the transformation of a .d.ts file to a .fsx file.
// Running test in debug mode with Vitest is not stable enough right now.
// So by using this script manually, we avoid avoid to re-start VSCode
// too often because of a wrong state of the Vitest extension.
//
//------------------------------------------------------------------------------

import { generateBindingFile } from '../../src/Glutinum.Converter/Generate.fs.js';
import path from 'node:path';
import * as Diff from 'diff';
import chalk from 'chalk';
import fs from 'fs';

const log = console.log;

if (process.argv.length < 3) {
log(chalk.red('Please provide the path to the definition file'));
log(chalk.red('Example: node --enable-source-maps index.js tests/specs/references/exports/variable.d.ts'));
process.exit(1);
}

const definitionFile = path.join(process.cwd(), process.argv[2]);

log(chalk.grey(`Reference: file://${definitionFile}`));

try {
let actual = generateBindingFile(definitionFile);
actual += `
(***)
#r "nuget: Fable.Core"
(***)
`;

const expectedFile = definitionFile.split('.').slice(0, -2).join('.') + '.fsx'
const expectedFileExists = fs.existsSync(expectedFile);
const expectedContent = expectedFileExists ? fs.readFileSync(expectedFile, 'utf8') : '';

log(chalk.grey(`Expected: file://${expectedFile}`));

console.log();
console.log('Generated content:\n');

const diff = Diff.diffChars(expectedContent, actual);

diff.forEach((part) => {
// green for additions, red for deletions
let text = part.added ? chalk.bgGreenBright(part.value) :
part.removed ? chalk.bgRedBright(part.value) :
part.value;
process.stderr.write(text);
});

console.log();

if (actual !== expectedContent) {
console.error(chalk.red('Actual content does not match expected content'));
process.exit(1);
} else {
console.log(chalk.green('Success'));
}
} catch (error) {
console.error(chalk.red(error.message));
process.exit(1);
}

0 comments on commit 90c2659

Please sign in to comment.