前回以下のコマンドでサンプルプロジェクトを作成したが、そのままではテストが通らなかった。
また、全部のパスを通るテストを追加してみたがカバレッジが100%に上がらなかったので対応

console
npx create-cloudflare sample-hono https://github.com/honojs/hono-minimal

そのまま実行した場合以下のようにエラーが出力された。

console
root@5ad01add5449:/workspace/sample-hono2# npm run test

> [email protected] test
> jest --verbose

 FAIL  src/index.test.ts
  ● Test suite failed to run

    ENOENT: no such file or directory, open '/workspace/sample-hono/dist/index.js'

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        5.37 s

またカバレッジは以下の通り

console
root@5ad01add5449:/workspace/sample-hono# npm run test

> [email protected] test
> jest --verbose --coverage

 PASS  src/index.test.ts (9.735 s)
  Test the application
    ✓ Should return 200 response (10 ms)
    ✓ Should return 401 response (1 ms)
    ✓ Should return 200 response (18 ms)
    ✓ Should return 200 response (2 ms)
    ✓ Should return 201 response (3 ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |   97.67 |    72.72 |    90.9 |   97.36 |
 index.ts |   97.67 |    72.72 |    90.9 |   97.36 | 21
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       5 passed, 5 total
Snapshots:   0 total
Time:        16.498 s
Ran all test suites.

対応

修正箇所は以下2つ

  • package.jsonのmainをsrc/index.tsに変更

  • esbuild-jestの設定にsourcemapを追記(※カバレッジ対応)

package.json
{
  "name": "sample-hono",
  "private": true,
  "version": "0.0.1",
  "main": "src/index.ts",
  "scripts": {
    "test": "jest --verbose --coverage",
    "dev": "wrangler dev src/index.ts",
    "deploy": "wrangler publish src/index.ts --minify",
    "wrangler": "wrangler"
  },
  "license": "MIT",
  "dependencies": {
    "hono": "^1.5.0"
  },
  "devDependencies": {
    "@cloudflare/workers-types": "^3.10.0",
    "@types/jest": "^28.1.0",
    "esbuild": "0.14.47",
    "esbuild-jest": "0.5.0",
    "jest": "^28.1.1",
    "jest-environment-miniflare": "^2.5.1",
    "miniflare": "^2.5.0",
    "wrangler": "^2.0.14"
  }
}
jest.config.js
module.exports = {
  testEnvironment: "miniflare",
  testMatch: [
    "**/test/**/*.+(ts|tsx|js)",
    "**/src/**/(*.)+(spec|test).+(ts|tsx|js)",
  ],
  transform: {
    "^.+\\.(ts|tsx)$": [
      "esbuild-jest",
      {
        sourcemap: true
      },
    ],
  },
  coverageDirectory: "reports/coverage",
  collectCoverage: true,
  coverageReporters: ["json", "lcov", "text", "html"],
  collectCoverageFrom: ["src/**/*.ts", "!**/node_modules/**"],
};

結果

console
root@5ad01add5449:/workspace/sample-hono# npm run test

> [email protected] test
> jest --verbose --coverage

 PASS  src/index.test.ts (12.36 s)
  Test the application
    ✓ Should return 200 response (9 ms)
    ✓ Should return 401 response (2 ms)
    ✓ Should return 200 response (23 ms)
    ✓ Should return 200 response (10 ms)
    ✓ Should return 201 response (3 ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |
 index.ts |     100 |      100 |     100 |     100 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       5 passed, 5 total
Snapshots:   0 total
Time:        24.842 s
Ran all test suites.