├── .npmignore ├── .prettierrc ├── src ├── index.ts └── patch-jest.ts ├── .babelrc ├── tsconfig.json ├── index.d.ts ├── LICENSE ├── package.json ├── tests ├── mac.test.ts ├── linux.test.ts └── windows.test.ts ├── .github └── dependabot.yml ├── .gitignore └── README.md /.npmignore: -------------------------------------------------------------------------------- 1 | /src/ 2 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | semi: false 2 | trailingComma: es5 3 | printWidth: 120 4 | singleQuote: true 5 | endOfLine: lf 6 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { platform } from 'os' 2 | import { patch } from './patch-jest' 3 | 4 | const currentPlatform = platform() 5 | patch(currentPlatform) 6 | -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | [ 4 | "@babel/preset-env", 5 | { 6 | "targets": { 7 | "node": "8.11.3" 8 | } 9 | }, 10 | ], ["@babel/preset-typescript"] 11 | ] 12 | } 13 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "target": "es5", 5 | "moduleResolution": "node", 6 | "outDir": "./lib", 7 | "noImplicitAny": true, 8 | "noImplicitReturns": true, 9 | "noImplicitThis": true, 10 | "strict": true, 11 | "types": ["node", "jest"] 12 | }, 13 | "include": ["./src/**/*"], 14 | "files": [ 15 | "index.d.ts" 16 | ] 17 | } 18 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | declare module jest { 2 | export interface Describe { 3 | onWindows: jest.Describe 4 | onMac: jest.Describe 5 | onLinux: jest.Describe 6 | skipWindows: jest.Describe 7 | skipMac: jest.Describe 8 | skipLinux: jest.Describe 9 | } 10 | 11 | export interface It { 12 | onWindows: jest.It 13 | onMac: jest.It 14 | onLinux: jest.It 15 | skipWindows: jest.It 16 | skipMac: jest.It 17 | skipLinux: jest.It 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Quentin Ménoret 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest-os-detection", 3 | "version": "1.3.1", 4 | "description": "Allow to specify on which platform to run jest tests", 5 | "main": "lib/index.js", 6 | "homepage": "https://github.com/doctolib/jest-os-detection", 7 | "repository": "github:doctolib/jest-os-detection", 8 | "types": "index.d.ts", 9 | "scripts": { 10 | "test": "jest", 11 | "version": "tsc --declaration src/* --outDir lib" 12 | }, 13 | "author": "qmenoret", 14 | "keywords": [ 15 | "jest", 16 | "windows", 17 | "linux", 18 | "mac", 19 | "os", 20 | "detection" 21 | ], 22 | "license": "MIT", 23 | "devDependencies": { 24 | "@babel/cli": "^7.0.0", 25 | "@babel/core": "^7.0.0", 26 | "@babel/node": "^7.0.0", 27 | "@babel/polyfill": "^7.0.0", 28 | "@babel/preset-env": "^7.0.0", 29 | "@babel/preset-typescript": "^7.10.4", 30 | "@babel/register": "^7.0.0", 31 | "@types/jest": "^26.0.4", 32 | "@types/node": "^15.6.2", 33 | "babel-core": "^7.0.0-bridge.0", 34 | "babel-jest": "^26.5.2", 35 | "babel-loader": "^8.0.4", 36 | "jest": "^26.5.0", 37 | "prettier": "^2.0.2", 38 | "typescript": "^4.0.3" 39 | }, 40 | "jest": { 41 | "roots": [ 42 | "/tests" 43 | ], 44 | "forceExit": true 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/patch-jest.ts: -------------------------------------------------------------------------------- 1 | interface Command { 2 | onWindows: ['win32'] 3 | onMac: ['darwin'] 4 | onLinux: ['linux'] 5 | skipWindows: ['linux', 'darwin'] 6 | skipMac: ['linux', 'win32'] 7 | skipLinux: ['darwin', 'win32'] 8 | } 9 | 10 | const AVAILABLE_PLATFORMS: Command = { 11 | onWindows: ['win32'], 12 | onMac: ['darwin'], 13 | onLinux: ['linux'], 14 | skipWindows: ['linux', 'darwin'], 15 | skipMac: ['linux', 'win32'], 16 | skipLinux: ['darwin', 'win32'], 17 | } 18 | 19 | type MethodOperation = 'skip' | 'only' 20 | 21 | const methodOperations: MethodOperation[] = ['skip', 'only'] 22 | 23 | export function patch(currentPlatform: NodeJS.Platform) { 24 | function newDefinition(method: T, expectedPlatform: keyof Command, fallbackImplem: T): T { 25 | if ((AVAILABLE_PLATFORMS[expectedPlatform] as NodeJS.Platform[] | undefined)?.indexOf(currentPlatform) !== -1) { 26 | return method 27 | } else { 28 | return fallbackImplem 29 | } 30 | } 31 | 32 | ;(Object.keys(AVAILABLE_PLATFORMS) as (keyof Command)[]).forEach((platform) => { 33 | ;[describe, it, test].forEach((method) => { 34 | method[platform] = newDefinition(method, platform, method.skip) 35 | method[platform].each = newDefinition(method.each, platform, method.skip.each) 36 | 37 | methodOperations.forEach((mode) => { 38 | method[platform][mode] = newDefinition(method[mode], platform, method.skip) 39 | method[platform][mode].each = newDefinition(method[mode].each, platform, method.skip.each) 40 | }) 41 | }) 42 | }) 43 | } 44 | -------------------------------------------------------------------------------- /tests/mac.test.ts: -------------------------------------------------------------------------------- 1 | import { patch } from '../src/patch-jest' 2 | 3 | patch('darwin') 4 | 5 | describe('Mac test suite', () => { 6 | describe('for "it"', () => { 7 | it.onMac('it.onMac', () => { 8 | expect(1).toBe(1) 9 | }) 10 | 11 | it.onMac.each([1])('it.onMac.each', value => { 12 | expect(value).toBe(1) 13 | }) 14 | 15 | it.onMac.skip.each([1])('it.onMac.skip.each', value => { 16 | throw new Error('Should not be run') 17 | }) 18 | 19 | it.onMac.skip('it.onMac.skip', () => { 20 | throw new Error('Should not be run') 21 | }) 22 | }) 23 | 24 | describe('for "test"', () => { 25 | test.onMac('test.onMac', () => { 26 | expect(1).toBe(1) 27 | }) 28 | 29 | test.onMac.each([1])('test.onMac.each', value => { 30 | expect(value).toBe(1) 31 | }) 32 | 33 | test.onMac.skip.each([1])('test.onMac.skip.each', value => { 34 | throw new Error('Should not be run') 35 | }) 36 | 37 | test.onMac.skip('test.onMac.skip', () => { 38 | throw new Error('Should not be run') 39 | }) 40 | }) 41 | 42 | describe.onMac('describe.onMac', () => { 43 | it('it', () => { 44 | expect(1).toBe(1) 45 | }) 46 | }) 47 | 48 | describe.onMac.each( 49 | [1] 50 | )('describe.onMac.each', value => { 51 | it('it', () => { 52 | expect(value).toBe(1) 53 | }) 54 | }) 55 | 56 | describe.onWindows('describe.onWindows', () => { 57 | it('it', () => { 58 | throw new Error('Should not be run') 59 | }) 60 | }) 61 | 62 | describe.onWindows.each( 63 | [1,2,3] 64 | )('describe.onWindows.each', () => { 65 | it('it', () => { 66 | throw new Error('Should not be run') 67 | }) 68 | }) 69 | 70 | describe.skipLinux('describe.skipLinux', () => { 71 | it('should be executed', () => { 72 | expect(1).toBe(1) 73 | }) 74 | }) 75 | }) 76 | -------------------------------------------------------------------------------- /tests/linux.test.ts: -------------------------------------------------------------------------------- 1 | import { patch } from '../src/patch-jest' 2 | 3 | patch('linux') 4 | 5 | describe('Linux test suite', () => { 6 | describe('for "it"', () => { 7 | it.onLinux('it.onLinux', () => { 8 | expect(1).toBe(1) 9 | }) 10 | 11 | it.onLinux.each([1])('it.onLinux.each', value => { 12 | expect(value).toBe(1) 13 | }) 14 | 15 | it.onLinux.skip.each([1])('it.onLinux.skip.each', value => { 16 | throw new Error('Should not be run') 17 | }) 18 | 19 | it.onLinux.skip('it.onLinux.skip', () => { 20 | throw new Error('Should not be run') 21 | }) 22 | }) 23 | 24 | describe('for "test"', () => { 25 | test.onLinux('test.onLinux', () => { 26 | expect(1).toBe(1) 27 | }) 28 | 29 | test.onLinux.each([1])('test.onLinux.each', value => { 30 | expect(value).toBe(1) 31 | }) 32 | 33 | test.onLinux.skip.each([1])('test.onLinux.skip.each', value => { 34 | throw new Error('Should not be run') 35 | }) 36 | 37 | test.onLinux.skip('test.onLinux.skip', () => { 38 | throw new Error('Should not be run') 39 | }) 40 | }) 41 | 42 | describe.onLinux('describe.onLinux', () => { 43 | it('it', () => { 44 | expect(1).toBe(1) 45 | }) 46 | }) 47 | 48 | describe.onLinux.each([1])('describe.onLinux.each', value => { 49 | it('it', () => { 50 | expect(value).toBe(1) 51 | }) 52 | }) 53 | 54 | describe.onWindows('describe.onWindows', () => { 55 | it('it', () => { 56 | throw new Error('Should not be run') 57 | }) 58 | }) 59 | 60 | describe.onWindows.each([1, 2, 3])('describe.onWindows.each', () => { 61 | it('it', () => { 62 | throw new Error('Should not be run') 63 | }) 64 | }) 65 | 66 | describe.skipLinux('describe.skipLinux', () => { 67 | it('should not be executed', () => { 68 | throw new Error('should not be run') 69 | }) 70 | }) 71 | }) 72 | -------------------------------------------------------------------------------- /tests/windows.test.ts: -------------------------------------------------------------------------------- 1 | import { patch } from '../src/patch-jest' 2 | 3 | patch('win32') 4 | 5 | describe('Windows test suite', () => { 6 | describe('for "it"', () => { 7 | it.onWindows('it.onWindows', () => { 8 | expect(1).toBe(1) 9 | }) 10 | 11 | it.onWindows.each([1])('it.onWindows.each', value => { 12 | expect(value).toBe(1) 13 | }) 14 | 15 | it.onWindows.skip.each([1])('it.onWindows.skip.each', value => { 16 | throw new Error('Should not be run') 17 | }) 18 | 19 | it.onWindows.skip('it.onWindows.skip', () => { 20 | throw new Error('Should not be run') 21 | }) 22 | }) 23 | 24 | describe('for "test"', () => { 25 | test.onWindows('test.onWindows', () => { 26 | expect(1).toBe(1) 27 | }) 28 | 29 | test.onWindows.each([1])('test.onWindows.each', value => { 30 | expect(value).toBe(1) 31 | }) 32 | 33 | test.onWindows.skip.each([1])('test.onWindows.skip.each', value => { 34 | throw new Error('Should not be run') 35 | }) 36 | 37 | test.onWindows.skip('test.onWindows.skip', () => { 38 | throw new Error('Should not be run') 39 | }) 40 | }) 41 | 42 | describe.onWindows('describe.onWindows', () => { 43 | it('it', () => { 44 | expect(1).toBe(1) 45 | }) 46 | }) 47 | 48 | describe.onWindows.each([1])('describe.onWindows.each', value => { 49 | it('it', () => { 50 | expect(value).toBe(1) 51 | }) 52 | }) 53 | 54 | describe.onMac('describe.onMac', () => { 55 | it('it', () => { 56 | throw new Error('Should not be run') 57 | }) 58 | }) 59 | 60 | describe.onMac.each([1, 2, 3])('describe.onMac.each', () => { 61 | it('it', () => { 62 | throw new Error('Should not be run') 63 | }) 64 | }) 65 | 66 | describe.skipLinux('describe.skipLinux', () => { 67 | it('should be executed', () => { 68 | expect(1).toBe(1) 69 | }) 70 | }) 71 | }) 72 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: npm 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | time: "01:00" 8 | pull-request-branch-name: 9 | separator: "-" 10 | open-pull-requests-limit: 99 11 | target-branch: master 12 | assignees: 13 | - qmenoret 14 | labels: 15 | - dependencies 16 | ignore: 17 | - dependency-name: "@types/node" 18 | versions: 19 | - 14.14.23 20 | - 14.14.24 21 | - 14.14.26 22 | - 14.14.27 23 | - 14.14.28 24 | - 14.14.29 25 | - 14.14.30 26 | - 14.14.31 27 | - 14.14.32 28 | - 14.14.33 29 | - 14.14.34 30 | - 14.14.35 31 | - 14.14.36 32 | - 14.14.37 33 | - 14.14.39 34 | - 14.14.41 35 | - 15.0.0 36 | - dependency-name: "@babel/core" 37 | versions: 38 | - 7.12.16 39 | - 7.12.17 40 | - 7.13.0 41 | - 7.13.1 42 | - 7.13.10 43 | - 7.13.13 44 | - 7.13.14 45 | - 7.13.15 46 | - 7.13.8 47 | - dependency-name: y18n 48 | versions: 49 | - 4.0.1 50 | - dependency-name: "@babel/cli" 51 | versions: 52 | - 7.12.16 53 | - 7.12.17 54 | - 7.13.0 55 | - 7.13.10 56 | - 7.13.14 57 | - dependency-name: "@babel/register" 58 | versions: 59 | - 7.12.13 60 | - 7.13.0 61 | - 7.13.14 62 | - 7.13.8 63 | - dependency-name: "@types/jest" 64 | versions: 65 | - 26.0.21 66 | - 26.0.22 67 | - dependency-name: "@babel/preset-env" 68 | versions: 69 | - 7.12.16 70 | - 7.12.17 71 | - 7.13.0 72 | - 7.13.10 73 | - 7.13.12 74 | - 7.13.5 75 | - 7.13.8 76 | - 7.13.9 77 | - dependency-name: "@babel/node" 78 | versions: 79 | - 7.12.16 80 | - 7.12.17 81 | - 7.13.0 82 | - 7.13.10 83 | - 7.13.12 84 | - dependency-name: typescript 85 | versions: 86 | - 4.1.4 87 | - 4.1.5 88 | - 4.2.2 89 | - 4.2.3 90 | - dependency-name: "@babel/preset-typescript" 91 | versions: 92 | - 7.12.16 93 | - 7.12.17 94 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # public 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | 106 | /lib/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Jest OS detection 2 | 3 | This module allows you to specify on which OS your tests should run. 4 | A common use case would be to have a CI running on different OS 5 | (say mac and windows) but you want to have all your tests in the same file. 6 | Unfortunately, some of your tests should only run on a specific platform 7 | due to OS specific features. 8 | 9 | All tests that should not be run on the current 10 | platform will be automatically skipped. 11 | 12 | ## Install 13 | 14 | ```bash 15 | # with npm 16 | npm install jest-os-detection 17 | 18 | # with yarn 19 | yarn add jest-os-detection 20 | ``` 21 | 22 | ## Setup 23 | 24 | In your package.json 25 | 26 | ```json 27 | "jest": { 28 | "setupFilesAfterEnv": ["jest-os-detection"], 29 | } 30 | ``` 31 | 32 | ## Usage 33 | 34 | ```js 35 | describe.onWindows('this describe is only interpreted on Windows', () => { 36 | it.onMac('this test is only interpreted on Mac', () => {}) 37 | test.onLinux('this test is only interpreted on Linux', () => {}) 38 | it.onMac.skip('this test is only interpreted on Mac but skipped', () => {}) 39 | test.onLinux.skip('this test is only interpreted on Linux but skipped', () => {}) 40 | it.onMac.only('only this test is executed on Mac', () => {}) 41 | test.onLinux.only('only this test is executed on Linux', () => {}) 42 | }) 43 | 44 | describe.onWindows.each([1, 2, 3])('several describe on windows', describeValue => { 45 | it.onMac.each([1, 2, 3, 4])('several tests on windows', testValue => {}) 46 | test.onLinux.each([1, 2, 3, 4])('several tests on windows', testValue => {}) 47 | it.onMac.each.skip([1, 2, 3, 4])('several tests skipped on windows', testValue => {}) 48 | test.onLinux.each.skip([1, 2, 3, 4])('several tests skipped on windows', testValue => {}) 49 | it.onMac.each.only([1, 2, 3, 4])('only these tests will be executed on windows', testValue => {}) 50 | test.onLinux.each.only([1, 2, 3, 4])('only these tests will be executed on windows', testValue => {}) 51 | }) 52 | ``` 53 | 54 | ## Supported features 55 | 56 | Supported commands: 57 | * `describe()` 58 | * `it()` 59 | * `test()` 60 | 61 | Supported platform: 62 | * `.onWindows()` 63 | * `.onMac()` 64 | * `.onLinux()` 65 | * `.skipWindows()` 66 | * `.skipMac()` 67 | * `.skipLinux()` 68 | 69 | Supported sub-commands: 70 | * `..each()` 71 | * `..only()` 72 | * `..skip()` 73 | * `..skip.each()` 74 | * `..only.each()` 75 | 76 | ## TypeScript 77 | 78 | To avoid type errors globally, you can add this to your tsconfig: 79 | ```json 80 | { 81 | "files": [ 82 | "node_modules/jest-os-detection/index.d.ts" 83 | ] 84 | } 85 | ``` 86 | --------------------------------------------------------------------------------