Skip to content

Commit

Permalink
feat(tests): set up unit tests ecosystem (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinezanardi committed Jan 7, 2024
1 parent ee3e55f commit e8dd711
Show file tree
Hide file tree
Showing 12 changed files with 710 additions and 8 deletions.
45 changes: 44 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,48 @@ jobs:
path: node_modules
key: ${{ runner.os }}-pnpm-v3-${{ hashFiles('pnpm-lock.yaml') }}

- name: Create Nuxt types ⛰️
run: pnpm run postinstall

- name: Build app ✨
run: pnpm run build
run: pnpm run build

unit-tests:
name: Unit Tests 🧪
runs-on: ubuntu-latest
needs:
- install
steps:
- name: Checkout GitHub repository 📡
uses: actions/checkout@v4

- name: Install pnpm 🏗️
uses: pnpm/action-setup@v2
with:
version: 8
run_install: false

- name: Setup NodeJS ✨
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: 'pnpm'

- name: Restore pnpm dependencies from cache 🥡
uses: actions/cache/restore@v3
id: cache-node-modules
with:
path: node_modules
key: ${{ runner.os }}-pnpm-v3-${{ hashFiles('pnpm-lock.yaml') }}

- name: Create Nuxt types ⛰️
run: pnpm run postinstall

- name: Unit tests 🧪
run: pnpm run test:unit:cov

- name: Save unit tests coverage report as artifact 💎
uses: actions/upload-artifact@v4
with:
name: unit-tests-coverage-report
path: tests/unit/coverage
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ logs
.env
.env.*
!.env.example

# Tests
tests/unit/coverage
13 changes: 13 additions & 0 deletions .run/Template Vitest.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="true" type="JavaScriptTestRunnerVitest">
<config value="$PROJECT_DIR$/config/vitest/vitest.unit-config.ts" />
<node-interpreter value="project" />
<vitest-package value="$PROJECT_DIR$/node_modules/vitest" />
<working-dir value="$PROJECT_DIR$" />
<envs>
<env name="NODE_ENV" value="test" />
</envs>
<scope-kind value="ALL" />
<method v="2" />
</configuration>
</component>
13 changes: 13 additions & 0 deletions .run/Unit Tests Coverage.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Unit Tests Coverage" type="js.build_tools.npm">
<package-json value="$PROJECT_DIR$/package.json" />
<command value="run" />
<scripts>
<script value="test:unit:cov" />
</scripts>
<node-interpreter value="project" />
<package-manager value="pnpm" />
<envs />
<method v="2" />
</configuration>
</component>
13 changes: 13 additions & 0 deletions .run/Unit Tests Watch.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Unit Tests Watch" type="js.build_tools.npm">
<package-json value="$PROJECT_DIR$/package.json" />
<command value="run" />
<scripts>
<script value="test:unit:watch" />
</scripts>
<node-interpreter value="project" />
<package-manager value="pnpm" />
<envs />
<method v="2" />
</configuration>
</component>
25 changes: 25 additions & 0 deletions .run/Unit Tests.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Unit Tests" type="JavaScriptTestRunnerVitest">
<config value="$PROJECT_DIR$/config/vitest/vitest.unit-config.ts" />
<node-interpreter value="project" />
<vitest-package value="$PROJECT_DIR$/node_modules/vitest" />
<working-dir value="$PROJECT_DIR$" />
<vitest-options value="--run --coverage" />
<envs>
<env name="NODE_ENV" value="test" />
</envs>
<scope-kind value="ALL" />
<method v="2" />
</configuration>
<configuration default="false" name="Unit Tests" type="js.build_tools.npm">
<package-json value="$PROJECT_DIR$/package.json" />
<command value="run" />
<scripts>
<script value="test:unit" />
</scripts>
<node-interpreter value="project" />
<package-manager value="pnpm" />
<envs />
<method v="2" />
</configuration>
</component>
37 changes: 37 additions & 0 deletions config/vitest/vitest.unit-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { defineVitestConfig } from "@nuxt/test-utils/config"

export default defineVitestConfig({
test: {
watch: false,
include: ["./tests/unit/**/*.spec.ts"],
exclude: [
"nuxt.config.ts",
"node_modules/**",
"config/**/*.ts",
],
coverage: {
exclude: [
"tests/**/*",
"node_modules/**/*",
"config/**/*",
],
include: [
"app.vue",
"components/**/*.[vue|ts]",
"composables/**/*.ts",
],
reportsDirectory: "./tests/unit/coverage",
all: true,
thresholds: {
lines: 100,
functions: 100,
branches: 100,
statements: 100,
}
},
globals: true,
clearMocks: true,
mockReset: true,
restoreMocks: true,
}
});
6 changes: 4 additions & 2 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
modules: [
"@nuxt/test-utils/module"
],
devtools: { enabled: true }
})
});
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,28 @@
"generate": "nuxt generate",
"preview": "nuxt preview",
"postinstall": "nuxt prepare",
"test:unit": "NODE_ENV=test vitest --config config/vitest/vitest.unit-config.ts",
"test:unit:watch": "NODE_ENV=test vitest --watch --config config/vitest/vitest.unit-config.ts",
"test:unit:cov": "NODE_ENV=test rimraf tests/unit/coverage && vitest --coverage --config config/vitest/vitest.unit-config.ts",
"script:create-branch": "scripts/create-git-branch.sh",
"script:create-pull-request": "scripts/create-pull-request.sh"
},
"devDependencies": {
"@commitlint/cli": "^18.4.4",
"@commitlint/config-conventional": "^18.4.4",
"@nuxt/test-utils": "^3.9.0",
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/git": "^10.0.1",
"@vitest/coverage-v8": "^1.1.3",
"@vue/test-utils": "^2.4.3",
"happy-dom": "^12.10.3",
"husky": "^8.0.3",
"nuxt": "^3.9.0",
"playwright-core": "^1.40.1",
"semantic-release": "^22.0.12",
"semantic-release-export-data": "^1.0.1",
"validate-branch-name": "^1.3.0",
"vitest": "^1.1.3",
"vue": "^3.4.5",
"vue-router": "^4.2.5"
},
Expand Down
Loading

0 comments on commit e8dd711

Please sign in to comment.