diff --git a/jest.config.js b/jest.config.js index 3d932fd0..26ebdd26 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,13 +1,16 @@ +const { join } = require('path'); + /* * For a detailed explanation regarding each configuration property and type check, visit: * https://jestjs.io/docs/configuration */ module.exports = { - restoreMocks: true, collectCoverage: true, coverageDirectory: 'coverage', coverageProvider: 'v8', + modulePathIgnorePatterns: ['/.build', '/dist/', '/build/'], preset: 'ts-jest', + restoreMocks: true, + setupFiles: [join(__dirname, './jest.setup.js')], testEnvironment: 'jest-environment-node', - modulePathIgnorePatterns: ['/.build', '/dist/', '/build/'], }; diff --git a/jest.setup.js b/jest.setup.js new file mode 100644 index 00000000..d2230d23 --- /dev/null +++ b/jest.setup.js @@ -0,0 +1,20 @@ +// Sometimes Jest can have issues when trying to spy on an internal module. Import +// and use this function as a workaround at the top of your test. +// +// Credit: https://github.com/facebook/jest/issues/6914#issuecomment-654710111 +const { defineProperty } = Object; + +Object.defineProperty = function (object, name, meta) { + if (meta.get && !meta.configurable) { + // it might be an ES6 exports object + return defineProperty(object, name, { + ...meta, + configurable: true, // prevent freezing + }); + } + + return defineProperty(object, name, meta); +}; + +// Disable logger if it is not explicitly set to true. +process.env.LOGGER_ENABLED = process.env.LOGGER_ENABLED || 'false'; diff --git a/packages/api/README.md b/packages/api/README.md index 2d2c62c6..5ab19fce 100644 --- a/packages/api/README.md +++ b/packages/api/README.md @@ -12,6 +12,18 @@ the data in memory and provides endpoints to push and retrieve beacon data. 2. `cp .env.example .env` - To copy the example environment variables. Optionally change the defaults. 3. `pnpm run dev` - To start the API server. The port number can be configured in the configuration file. +### Testing + +To run the tests: + +```sh +pnpm run test +# or to run test only from a specific files (path substring search) +pnpm run test schema +# or to enable logger (by default the logger is disabled by jest.setup.js). +LOGGER_ENABLED=true pnpm run test +``` + ## Configuration The API is configured via combination of [environment variables](#environment-variables) and diff --git a/packages/pusher/README.md b/packages/pusher/README.md index 750370ad..0476a96c 100644 --- a/packages/pusher/README.md +++ b/packages/pusher/README.md @@ -29,6 +29,18 @@ To start the the pusher in dev mode run the following: 5. `pnpm run dev` - To run the pusher. This step assumes already running signed API as specified in the `pusher.json` configuration. +### Testing + +To run the tests: + +```sh +pnpm run test +# or to run test only from a specific files (path substring search) +pnpm run test schema +# or to enable logger (by default the logger is disabled by jest.setup.js). +LOGGER_ENABLED=true pnpm run test +``` + ## Configuration Pusher can be configured via a combination of [environment variables](#environment-variables) and