{"id":12944,"library":"can-test-helpers","title":"CanJS Test Helpers","description":"CanJS Test Helpers is a utility package designed to provide common testing functions specifically for modules within the CanJS ecosystem. Currently at version 1.1.4, its primary features include `willWarn` and `willError` for intercepting and asserting against `canDev.warn` and `canDev.error` calls, and `devOnlyTest` for defining tests that execute only in development environments. The package ensures compatibility with modern testing setups by requiring QUnit 2.x since version 1.1.4. Its release cadence is generally aligned with updates in the broader CanJS family of libraries, such as `can-log` and `can-util`, ensuring seamless integration. A key differentiator is its direct interaction with CanJS's internal development logging mechanisms, providing fine-grained control over testing expected warnings and errors, which is crucial for library maintainers and application developers building with CanJS.","status":"active","version":"1.1.4","language":"javascript","source_language":"en","source_url":"git://github.com/canjs/can-test-helpers","tags":["javascript","canjs","donejs","qunit"],"install":[{"cmd":"npm install can-test-helpers","lang":"bash","label":"npm"},{"cmd":"yarn add can-test-helpers","lang":"bash","label":"yarn"},{"cmd":"pnpm add can-test-helpers","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary export is a default object containing `willWarn`, `willError`, and `devOnlyTest`.","wrong":"const devHelpers = require('can-test-helpers/lib/dev');","symbol":"devHelpers","correct":"import devHelpers from 'can-test-helpers/lib/dev';"},{"note":"Individual helpers are properties of the default `devHelpers` object, not direct named exports.","wrong":"import { willWarn } from 'can-test-helpers/lib/dev';","symbol":"willWarn","correct":"import devHelpers from 'can-test-helpers/lib/dev'; devHelpers.willWarn(...);"},{"note":"`devOnlyTest` is a utility for QUnit that defines a test to run only in a development build.","symbol":"devOnlyTest","correct":"import devHelpers from 'can-test-helpers/lib/dev'; devHelpers.devOnlyTest(...);"}],"quickstart":{"code":"// For demonstration purposes, mock canDev if not in a full CanJS environment.\n// In a real CanJS app, 'can-log/dev/dev' would be imported or globally available.\nconst canDev = {\n  warn: (message: string) => console.warn(`canDev.warn: ${message}`),\n  error: (message: string) => console.error(`canDev.error: ${message}`)\n};\n\nimport devHelpers from 'can-test-helpers/lib/dev';\n\n// Example 1: Testing expected warnings\nconst finishWarningCheck = devHelpers.willWarn(\"Something deprecated\", (message, match) => {\n  console.log(`Intercepted warning: \"${message}\" (matched: ${match})`);\n});\n\ncanDev.warn(\"Something deprecated\");\ncanDev.warn(\"Another unrelated warning\");\n\nconst matchedWarnings = finishWarningCheck();\nconsole.assert(matchedWarnings === 1, `Expected 1 matched warning, got ${matchedWarnings}`);\n\n// Example 2: Testing expected errors\nconst finishErrorCheck = devHelpers.willError(/critical error/i);\n\ntry {\n  // Simulate an error being caught and logged by canDev\n  throw new Error(\"A critical error occurred!\");\n} catch (e) {\n  canDev.error((e as Error).message);\n}\n\nconst matchedErrors = finishErrorCheck();\nconsole.assert(matchedErrors === 1, `Expected 1 matched error, got ${matchedErrors}`);\n\nconsole.log(\"Quickstart example completed. Check console output for assertions.\");\n","lang":"typescript","description":"Demonstrates how to use `willWarn` and `willError` helpers to intercept and assert against development warnings and errors in a CanJS context."},"warnings":[{"fix":"Upgrade QUnit in your project to v2 or higher. Adjust any QUnit 1.x specific assertions if necessary.","message":"can-test-helpers v1.1.4 and above require QUnit 2.x. Projects using older QUnit versions will experience test failures or `QUnit` API incompatibility.","severity":"breaking","affected_versions":">=1.1.4"},{"fix":"Ensure `can-log/dev/dev` is a dependency of your project and correctly imported or globally available where CanJS is used, or provide a mock `canDev` object with `warn` and `error` methods.","message":"The `willWarn` and `willError` helpers are designed to interact with `can-log/dev/dev`. For these helpers to function correctly, `can-log/dev/dev` must be installed and properly configured in your test environment, or `canDev` must be appropriately mocked.","severity":"gotcha","affected_versions":">=1.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Upgrade QUnit to version 2 or higher in your project's `devDependencies`. If using a test runner, ensure it's configured to use the updated QUnit.","cause":"Your test environment is likely using an outdated QUnit version (e.g., QUnit 1.x) while `can-test-helpers` expects QUnit 2.x.","error":"TypeError: QUnit.test is not a function"},{"fix":"Verify that `can-log/dev/dev` is installed (`npm install can-log --save-dev`) and correctly imported or globally available in your test setup. If not running a full CanJS app, you might need to mock `global.canDev` with `warn` and `error` methods.","cause":"The `canDev` object (from `can-log/dev/dev`) is not correctly imported, installed, or exposed in your test environment, preventing `can-test-helpers` from stubbing its methods.","error":"TypeError: canDev.warn is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}