diff --git a/.github/workflows/publish-to-npm.yml b/.github/workflows/publish-to-npm.yml index 2a273c6..d21edaf 100644 --- a/.github/workflows/publish-to-npm.yml +++ b/.github/workflows/publish-to-npm.yml @@ -19,6 +19,8 @@ jobs: cache: npm - run: npm ci - run: npm run test:unit -- --runInBand + # Tests build dist/ with source maps; rebuild without them before packing. + - run: npm run build - run: npm pack --dry-run - run: npm publish --access public env: diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index cf51226..5e0ca32 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -5,9 +5,12 @@ ```bash npm ci npm test -- --runInBand +npm run build npm pack --dry-run ``` +Tests build `dist/` with source maps for coverage. Run `npm run build` before `npm pack` so the package contains no `.map` files. `npm publish` does this automatically via `prepublishOnly`. + Real API tests require `CAPTCHA_API_KEY` and the target variables documented in `tests/README.md`: ```bash diff --git a/jest.config.js b/jest.config.js index 9706809..f607935 100644 --- a/jest.config.js +++ b/jest.config.js @@ -9,9 +9,14 @@ export default { testEnvironment: 'node', - // Collect coverage from every file in src/, not just the ones imported - // by tests. Otherwise a new file that nothing imports yet would silently - // drop out of the report instead of showing up as 0%. + // Tests import the built package from dist/, so that is what gets + // instrumented. `npm run build:test` emits source maps, which map the + // report back to src/*.ts instead of hashed dist/chunk-*.js files. + // + // Only the ESM build (*.js) is listed: the CommonJS build (*.cjs) is loaded + // through Node's native require() and is never instrumented by Jest, so + // listing it would only add bogus 0% entries. public-api.test.js covers it + // with smoke tests instead. collectCoverageFrom: ['dist/**/*.js'], // text -- console output, lcov -- for uploading to Coveralls. diff --git a/package.json b/package.json index aca194e..d09b9e9 100644 --- a/package.json +++ b/package.json @@ -26,11 +26,12 @@ ], "scripts": { "build": "tsup src/index.ts src/tasks.ts src/exceptions.ts --format esm,cjs --dts --clean", + "build:test": "npm run build -- --sourcemap", "typecheck": "tsc --noEmit", - "test": "npm run build && node --experimental-vm-modules node_modules/jest/bin/jest.js", - "test:unit": "npm run build && node --experimental-vm-modules node_modules/jest/bin/jest.js tests/unit", - "test:integration": "npm run build && node --experimental-vm-modules node_modules/jest/bin/jest.js tests/integration", - "prepublishOnly": "npm run test:unit -- --runInBand" + "test": "npm run build:test && node --experimental-vm-modules node_modules/jest/bin/jest.js", + "test:unit": "npm run build:test && node --experimental-vm-modules node_modules/jest/bin/jest.js tests/unit", + "test:integration": "npm run build:test && node --experimental-vm-modules node_modules/jest/bin/jest.js tests/integration", + "prepublishOnly": "npm run test:unit -- --runInBand && npm run build" }, "keywords": [ "captcha-solver", diff --git a/tests/README.md b/tests/README.md index a645d95..ff802f7 100644 --- a/tests/README.md +++ b/tests/README.md @@ -388,20 +388,20 @@ Current state: ``` File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s -All files | 97.53 | 83.63 | 100 | 97.5 | - client.js | 95.45 | 80.76 | 100 | 95.34 | 35,53 - exceptions.js | 100 | 100 | 100 | 100 | - index.js | 100 | 100 | 100 | 100 | - tasks.js | 100 | 84.52 | 100 | 100 | 26,38,71-77,89,107 +All files | 98.4 | 87.5 | 100 | 100 | + client.ts | 97.67 | 96.66 | 100 | 100 | 195 + exceptions.ts | 100 | 100 | 100 | 100 | + tasks.ts | 98.41 | 85.08 | 100 | 100 | 28,232,284-493,579-623,682-695 + version.ts | 100 | 100 | 100 | 100 | ``` -What is uncovered: the HTTP error branch (`!response.ok`) and the `AbortError` handling in `_request()`, plus some default parameter values in the task constructors. +What is uncovered: the second timeout check in `solve()` (it needs fake timers to hit) and default parameter values in the task constructors. The figure counts **unit tests only**. Integration is deliberately left out: without a key those suites skip, and the number would swing depending on whether `CAPTCHA_API_KEY` happened to be available. Two settings in [jest.config.js](../jest.config.js) matter specifically for coverage: -- `collectCoverageFrom: ['dist/**/*.{js,cjs}']` — count every JavaScript file in the built `dist/` package, including the public entry point, even when a test does not import it directly. +- `collectCoverageFrom: ['dist/**/*.js']` — instrument the built ESM package in `dist/`, since that is what the tests import. `npm run build:test` emits source maps, so the report is mapped back to `src/*.ts` instead of hashed `dist/chunk-*.js` files, and the file list stays stable in Coveralls. The CommonJS build (`*.cjs`) is loaded through Node's native `require()` and is never instrumented by Jest, so it is not listed; `public-api.test.js` covers it with smoke tests instead. - `coverageReporters: ['text', 'lcov']` — `text` for the console, `lcov` for the Coveralls upload from CI. Artifacts are written to `coverage/` (gitignored).