Skip to content

Commit

Permalink
Merge pull request #11 from classmodel/test-node-playwright
Browse files Browse the repository at this point in the history
Replace jest with test:node + add playwright
  • Loading branch information
Peter9192 authored Aug 8, 2024
2 parents b0e3011 + ee9d466 commit 29413e5
Show file tree
Hide file tree
Showing 13 changed files with 293 additions and 1,796 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Tests

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
unittest:
name: Unit tests
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: "20"
cache: "pnpm"
cache-dependency-path: ./pnpm-lock.yaml
- name: Install dependencies
run: pnpm install
- name: Test
run: pnpm test
e2etest:
name: end-to-end tests
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: "20"
cache: "pnpm"
cache-dependency-path: ./pnpm-lock.yaml
- name: Install dependencies
run: pnpm install
- name: Install Playwright Browsers
run: pnpm exec playwright install --with-deps chromium
working-directory: ./apps/class-solid
- name: Run Playwright tests
run: pnpm exec playwright test
working-directory: ./apps/class-solid
- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report
path: apps/class-solid/playwright-report/
retention-days: 30
32 changes: 29 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,34 @@ pnpm exec biome --help

To check types, you can run the `pnpm typecheck` command as other commands ignore types.

## Tests

The unit tests are written with [node:test](https://nodejs.org/api/test.html) and [node:assert](https://nodejs.org/api/assert.html).

The unit tests can be run with the following command:

```shell
pnpm test
```

To get test coverage

```shell
# Does not work via pnpm script so need to call node directly
node --import tsx --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=lcov.info src/*.test.ts
# To generate a html report use genhtml which is part of lcov OS package
genhtml lcov.info --output-directory coverage
```

The end-to-end tests are written with [playwright](https://playwright.dev/).
The tests are in `apps/class-solid/tests/*.spec.ts` and can be run with the following command:

```shell
cd ./apps/class-solid
pnpm exec playwright install # first time only
pnpm exec playwright test
```

## Tech stack

The CLASS package is written in typescript. It uses [zod](https://zod.dev/) for
Expand Down Expand Up @@ -92,6 +120,4 @@ To format and lint the code, we use [biome](https://biomejs.dev/) as it combines
- Use [AssemblyScript](https://www.assemblyscript.org/) or
[rust](https://www.rust-lang.org/what/wasm) for a faster implementation of
CLASS running on web assembly.
- Test with node test runner rather than jest (for the package) and/or use
[playwright](https://playwright.dev/) and/or
[storybook](https://storybook.js.org/) (for the web app)
- Use [storybook](https://storybook.js.org/) for UI component package
6 changes: 6 additions & 0 deletions apps/class-solid/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ gitignore
# System Files
.DS_Store
Thumbs.db

# Playwright
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
2 changes: 2 additions & 0 deletions apps/class-solid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"node": ">=18"
},
"devDependencies": {
"@playwright/test": "^1.45.3",
"@types/node": "^20.13.1",
"typescript": "^5.3.3",
"serve": "^14.2.3"
}
Expand Down
79 changes: 79 additions & 0 deletions apps/class-solid/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { defineConfig, devices } from "@playwright/test";

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// import dotenv from 'dotenv';
// dotenv.config({ path: path.resolve(__dirname, '.env') });

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: "./tests",
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: "html",
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
baseURL: "http://localhost:3000",

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: "on-first-retry",
},

/* Configure projects for major browsers */
projects: [
{
name: "chromium",
use: { ...devices["Desktop Chrome"] },
},

// {
// name: 'firefox',
// use: { ...devices['Desktop Firefox'] },
// },

// {
// name: 'webkit',
// use: { ...devices['Desktop Safari'] },
// },

/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },

/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],

/* Run your local dev server before starting the tests */
webServer: {
command: "pnpm dev",
cwd: "../..",
url: "http://localhost:3000",
reuseExistingServer: !process.env.CI,
},
});
9 changes: 9 additions & 0 deletions apps/class-solid/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { expect, test } from "@playwright/test";

test("has welcome", async ({ page }) => {
await page.goto("/");

await expect(
page.getByRole("heading", { name: "Welcome to CLASS" }),
).toBeVisible();
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"dev": "turbo dev",
"format-and-lint": "biome check .",
"format-and-lint:fix": "biome check . --write",
"typecheck": "turbo typecheck"
"typecheck": "turbo typecheck",
"test": "turbo test"
},
"devDependencies": {
"@biomejs/biome": "1.8.3",
Expand Down
2 changes: 2 additions & 0 deletions packages/class/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lcov.info
coverage/
7 changes: 0 additions & 7 deletions packages/class/jest.config.js

This file was deleted.

11 changes: 6 additions & 5 deletions packages/class/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@
},
"license": "GPL-3.0-only",
"scripts": {
"test": "jest",
"test:watch": "jext --watch",
"test": "tsx --test src/*.test.ts",
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"@types/jest": "^29.5.12",
"jest": "^29.7.0",
"ts-jest": "^29.2.2",
"@types/node": "^20.13.1",
"tsx": "^4.16.5",
"typescript": "^5.3.3"
},
"dependencies": {
"zod": "^3.23.8",
"zod-to-json-schema": "^3.23.1"
},
"engines": {
"node": ">=20.16.0"
}
}
19 changes: 19 additions & 0 deletions packages/class/src/bmi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import assert from "node:assert/strict";
import { before, describe, test } from "node:test";
import { BmiClass } from "./bmi";

describe("BmiClass", () => {
let bmi: BmiClass;
before(() => {
bmi = new BmiClass();
});

describe("get_component_name", () => {
test("returns the component name", () => {
assert.strictEqual(
bmi.get_component_name(),
"Chemistry Land-surface Atmosphere Soil Slab model",
);
});
});
});
27 changes: 15 additions & 12 deletions packages/class/src/class.test.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
import assert from "node:assert/strict";
import { describe, test } from "node:test";

import { CLASS } from "./class";
import { classConfig } from "./config";
import { runClass } from "./runner";

describe("CLASS model", () => {
it("can be instantiated with default config", () => {
test("can be instantiated with default config", () => {
const config = classConfig.parse({});
const model = new CLASS(config);
expect(model).toBeInstanceOf(CLASS);
expect(model.t).toEqual(0);
expect(model._cfg.initialState.h_0).toEqual(200);
expect(model._cfg.timeControl.dt).toEqual(60);
expect(model._cfg.mixedLayer.wtheta).toEqual(0.1);
expect(model._cfg.mixedLayer.wq).toEqual(0.0001);
assert.ok(model instanceof CLASS);
assert.strictEqual(model.t, 0);
assert.strictEqual(model._cfg.initialState.h_0, 200);
assert.strictEqual(model._cfg.timeControl.dt, 60);
assert.strictEqual(model._cfg.mixedLayer.wtheta, 0.1);
assert.strictEqual(model._cfg.mixedLayer.wq, 0.0001);
});

test("calling update advances the model time", () => {
const config = classConfig.parse({});
const model = new CLASS(config);
model.update();
expect(model.t).toEqual(60);
assert.strictEqual(model.t, 60);
});

it("can update until the final time step", () => {
test("can update until the final time step", () => {
const config = classConfig.parse({});
const model = new CLASS(config);
while (model.t < config.timeControl.runtime) {
model.update();
}
expect(model.t).toEqual(12 * 3600);
assert.strictEqual(model.t, 12 * 3600);
});

it("produces realistic results", () => {
test("produces realistic results", () => {
const config = classConfig.parse({});
const output = runClass(config);
console.log(output);
expect(output);
assert.ok(output);
});
});
Loading

0 comments on commit 29413e5

Please sign in to comment.