Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle server-only tests #108

Merged
merged 2 commits into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 94 additions & 74 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
// No need to use ts-jest https://github.com/vercel/next.js/discussions/13528#discussioncomment-22933
// Client-side Jest config
// You might need to setup another config file for server tests
module.exports = {
// preset: "ts-jest",
testEnvironment: "jsdom", // might need to change for Node on the server

// configuration that must be set for each project but does not change
const commonConfig = {
// A map from regular expressions to paths to transformers
// transform: undefined,
transform: {
//"^.+\\.[jt]sx?$": "ts-jest",
"^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
// MDX support
"^.+\\.(md|mdx)$": "jest-transformer-mdx",
},

// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
testPathIgnorePatterns: [
"/node_modules/",
"/cypress/",
"/storybook/",
"/.next/",
"/stories/",
],

// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
transformIgnorePatterns: [
'/node_modules/',
'^.+\\.module\\.(css|sass|scss)$',
],

// A map from regular expressions to module names that allow to stub out resources with a single module
moduleNameMapper: {
"~/(.*)": "<rootDir>/src/$1",
"@vulcanjs/(.*)": "<rootDir>/packages/@vulcanjs/$1",
},

// The directory where Jest should output its coverage files
coverageDirectory: "coverage-unit",

// All imported modules in your tests should be mocked automatically
// automock: false,

Expand All @@ -22,29 +53,6 @@ module.exports = {
// Indicates whether the coverage information should be collected while executing the test
// collectCoverage: false,

// An array of glob patterns indicating a set of files for which coverage information should be collected
// collectCoverageFrom: undefined,
collectCoverageFrom: [
"src/**/*.{js,jsx,ts,tsx}",
"!**/*.d.ts",
"!**/stor{y,ies}.{js,ts,jsx,tsx}",
"!**/*.stor{y,ies}.{js,ts,jsx,tsx}",
"!**/node_modules/**",
"!**/cypress/**",
"!**/.storybook/**",
"!**/stories/**",
"!**/storybook-static/**",
"!jest.config.js",
"!**/out/**",
"!**/dist/**",
"!**/public/**",
"!**/build/**",
"!**/coverage/**",
],

// The directory where Jest should output its coverage files
coverageDirectory: "coverage-unit",

// An array of regexp pattern strings used to skip coverage collection
// coveragePathIgnorePatterns: [
// "/node_modules/"
Expand All @@ -61,21 +69,12 @@ module.exports = {
// An object that configures minimum threshold enforcement for coverage results
// coverageThreshold: undefined,

// A path to a custom dependency extractor
// dependencyExtractor: undefined,

// Make calling deprecated APIs throw helpful error messages
// errorOnDeprecated: false,

// Force coverage collection from ignored files using an array of glob patterns
// forceCoverageMatch: [],

// A path to a module which exports an async function that is triggered once before all test suites
globalSetup: "./tests/globalSetup",

// A path to a module which exports an async function that is triggered once after all test suites
// globalTeardown: undefined,

// A set of global variables that need to be available in all test environments
//globals: {
// "ts-jest": {
Expand All @@ -91,14 +90,15 @@ module.exports = {
// "node_modules"
// ],

// A path to a custom dependency extractor
// dependencyExtractor: undefined,

// A path to a module which exports an async function that is triggered once before all test suites
globalSetup: "./tests/globalSetup",

// An array of file extensions your modules use
moduleFileExtensions: ["js", "json", "jsx", "ts", "tsx", "node"],

// A map from regular expressions to module names that allow to stub out resources with a single module
moduleNameMapper: {
"~/(.*)": "<rootDir>/src/$1",
"@vulcanjs/(.*)": "<rootDir>/packages/@vulcanjs/$1",
},

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],
Expand Down Expand Up @@ -142,9 +142,6 @@ module.exports = {
// The paths to modules that run some code to configure or set up the testing environment before each test
// setupFiles: [],

// A list of paths to modules that run some code to configure or set up the testing framework before each test
setupFilesAfterEnv: ["./tests/setupTests.js"],

// A list of paths to snapshot serializer modules Jest should use for snapshot testing
// snapshotSerializers: [],

Expand All @@ -157,20 +154,6 @@ module.exports = {
// Adds a location field to test results
// testLocationInResults: false,

// The glob patterns Jest uses to detect test files
// testMatch: [
// "**/__tests__/**/*.[jt]s?(x)",
// "**/?(*.)+(spec|test).[tj]s?(x)"
// ],

// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
testPathIgnorePatterns: [
"/node_modules/",
"/cypress/",
"/storybook/",
"/.next/",
"/stories/",
],

// The regexp pattern or array of patterns that Jest uses to detect test files
// testRegex: [],
Expand All @@ -187,20 +170,6 @@ module.exports = {
// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
// timers: "real",

// A map from regular expressions to paths to transformers
// transform: undefined,
transform: {
//"^.+\\.[jt]sx?$": "ts-jest",
"^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
// MDX support
"^.+\\.(md|mdx)$": "jest-transformer-mdx",
},

// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
// transformIgnorePatterns: [
// "/node_modules/"
// ],

// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
// unmockedModulePathPatterns: undefined,

Expand All @@ -212,4 +181,55 @@ module.exports = {

// Whether to use watchman for file crawling
// watchman: true,
};
}

module.exports = {
// An array of glob patterns indicating a set of files for which coverage information should be collected
collectCoverageFrom: [
"src/**/*.{js,jsx,ts,tsx}",
"!**/*.d.ts",
"!**/stor{y,ies}.{js,ts,jsx,tsx}",
"!**/*.stor{y,ies}.{js,ts,jsx,tsx}",
"!**/node_modules/**",
"!**/cypress/**",
"!**/.storybook/**",
"!**/stories/**",
"!**/storybook-static/**",
"!jest.config.js",
"!**/out/**",
"!**/dist/**",
"!**/public/**",
"!**/build/**",
"!**/coverage/**",
],

// configuration for each environment (client or server)
projects: [
{
...commonConfig,
name: 'client',
displayName: 'client',
// testEnvironment: "jsdom", // defautl already
// The glob patterns Jest uses to detect test files
testMatch: [
'**/__tests__/(!server)/**/*.[jt]s?(x)',
'**/__tests__/*.[jt]s?(x)',
'**/!(*.server).test.[jt]s?(x)',
],
// A list of paths to modules that run some code to configure or set up the testing framework before each test
setupFilesAfterEnv: ["./tests/setupTests.js"], modulePaths: ['<rootDir>'],
},
{
...commonConfig,
name: 'server',
displayName: 'server',
testEnvironment: 'node',
// The glob patterns Jest uses to detect test files
testMatch: [
'**/__tests__/server/**/*.[jt]s?(x)',
'**/*.server.test.[jt]s?(x)',
],
setupFilesAfterEnv: ["./tests/setupTests.server.js"], modulePaths: ['<rootDir>'],
},
],
}
5 changes: 5 additions & 0 deletions tests/configTests/exampleClient.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('Default test (collocated with code)', () => {
test('Default test is run in a JSDOM environment', () => {
expect(window).toBeDefined()
})
})
9 changes: 9 additions & 0 deletions tests/configTests/exampleServer.server.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
describe('Server-only test (collocated with code)', () => {
// Since jsdom also has node definied, this test is more to be sure that server tests will run than to see that it isn't processed as a client.
test('server-only test is run in a Node environment', () => {
expect(process?.versions?.node).toBeDefined()
})
test('server-only test is NOT run in a JSDOM environment', () => {
expect(typeof window).toBe('undefined')
})
})
1 change: 1 addition & 0 deletions tests/setupTests.server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Put here your code configuring or setting up server testing environment