Install jest(it needs Jest 21+) and jest-runner-phpunit
# jest-runner-phpunit assumes phpunit is installed with composer
composer require --dev phpunit/phpunit
# Install with yarn
yarn add --dev jest jest-runner-phpunit
# or with NPM
npm install --save-dev jest jest-runner-phpunitIn your package.json
{
"jest": {
"runner": "jest-runner-phpunit",
"displayName": "phpunit",
"moduleFileExtensions": ["php"],
"testMatch": ["<rootDir>/tests/**/*Test.php"]
}
}Or in jest.config.js
module.exports = {
runner: 'jest-runner-phpunit',
"displayName": "phpunit",
"moduleFileExtensions": ["php"],
testMatch: ['<rootDir>/tests/**/*Test.php'],
}Please update testMatch to match your project folder structure
It is recommended to use the projects configuration option to run multiple Jest runners simultaneously.
If you are using Jest <22.0.5, you can use multiple Jest configuration files and supply the paths to those files in the projects option. For example:
// jest-test.config.js
module.exports = {
// your Jest test options
displayName: 'test'
}
// jest-phpunit.config.js
module.exports = {
// your jest-runner-phpunit options
runner: 'jest-runner-phpunit',
displayName: "phpunit",
moduleFileExtensions: ["php"],
testMatch: ['<rootDir>/tests/**/*Test.php']
}In your package.json:
{
"jest": {
"projects": [
"<rootDir>/jest-test.config.js",
"<rootDir>/jest-phpunit.config.js"
]
}
}Or in jest.config.js:
module.exports = {
projects: [
'<rootDir>/jest-test.config.js',
'<rootDir>/jest-phpunit.config.js'
]
}If you are using Jest >=22.0.5, you can supply an array of project configuration objects instead. In your package.json:
{
"jest": {
"projects": [
{
"displayName": "test"
},
{
"runner": "jest-runner-phpunit",
"displayName": "phpunit",
"moduleFileExtensions": ["php"],
"testMatch": ["<rootDir>/tests/**/*Test.php"]
}
]
}
}Or in jest.config.js:
module.exports = {
projects: [
{
displayName: 'test'
},
{
runner: 'jest-runner-phpunit',
displayName: "phpunit",
moduleFileExtensions: ["php"],
testMatch: ['<rootDir>/tests/**/*Test.php']
}
]
}yarn jest