Closed chrismilleruk closed 3 years ago
Thanks for providing the sample repo. Most of your question/issue is not Wallaby specific (you need to configure Jest and Wallaby should just work with the working Jest config).
Wallaby autodetect supports selecting subfolders (with Jest configured), so if you can run Jest in a folder with npx jest
, then Wallaby can run it too (without any additional Wallaby config files).
Your /amplify/backend/function/amplifyFunction1
is almost good in that regard, it just has an invalid amplify/backend/function/amplifyFunction1/jest.config.js
(the file doesn't export the config). After you add
+module.exports = config;
at the end of the file, you will be able to run npx jest
in the folder /amplify/backend/function/amplifyFunction1
, and Wallaby autodetect for that folder will also work for you (Wallaby.js: Select Configuration
- then select amplify/backend/function/amplifyFunction1/
):
In an ideal world, there would be a single wallaby config which covers all tests so we don't break flow by switching configs. In 2nd place, having a single wallaby config to cover everything in ./amplify/backend/function so that it could trigger all the lambda function tests at once. I attempted this with ./wallaby.config.amplify.backend.js
As I have mentioned above, if you can run Jest in any folder with npx jest
, then Wallaby can run it too (without any additional Wallaby config files). So the same way as for the amplifyFunction1
subfolder above, you will just need to add a working jest config file into the amplify/backend/function/
folder and select the folder using Wallaby command. The same way, if you find a way to add a working Jest config in your repo's root, so that when you run npx jest
in your repo's root - it runs all of your tests, then Wallaby autodetect for the root will also run that config and will run all of your tests.
You may use Jest multi project setting to specify multiple projects to run tests in in a single Jest config. For example, if you copy/paste your amplifyFunction1
folder so that you have multiple functions to test, and then add the following file amplify/backend/jest.config.js
module.exports = {
projects: [
'<rootDir>/function/*'
]
}
then cd amplify/backend/
and npx jest
, all of your function projects will run with Jest (and Wallaby too):
Creating your Jest config for the root with CRA and some other subfolder to run all tests is a bit tricker (because CRA gets some custom Jest configuration resolved) but should be possible too:
jest
as a dev dependency to your package.json: npm i --save-dev jest
,npm test
script to run jest
as opposed to react-scripts
,jest.config.js
in your repo's root:
module.exports = {
projects: [
{
rootDir: '<rootDir>',
...require('react-scripts/scripts/utils/createJestConfig.js')(p => require('path').resolve('./node_modules/react-scripts', p))
},
'<rootDir>/amplify/backend/function/*'
]
};
Now npx jest
in your repo's root (as well as Wallaby autodetect for your repo's root) should work too:
Hope it helps and answers your question. I have created the pull request with Jest configs and other changes mentioned in the response: https://github.com/chrismilleruk/amplify-wallaby/pull/1.
Thanks for your help and quick response!!
Issue description or question
Project setup & background
https://github.com/chrismilleruk/amplify-wallaby
This vanilla project structure is essentially a Create React App with an
amplify
folder as created with the Amplify CLI viaamplify init
.I believe the Create React App is fairly standard in
package.json
and./src
and works with Wallaby using autodetect. The CRA has not been ejected.Lambda functions can be added to
./amplify/backend/function/<fnName>
with Amplify CLI Function viaamplify add function
.I have added some basic tests to the
hello world
lambda function and added TS support via ts-jest. Those tests run OK with the following code:cd amplify/backend/function/amplifyFunction1 && jest --preset ts-jest --watch && cd -
ornpm run test:functions
Issue experienced
I'm struggling to configure Wallaby in a way that works for both the CRA tests and the ts-node Lambda Function tests.
In an ideal world, there would be a single wallaby config which covers all tests so we don't break flow by switching configs. Since wallaby doesn't necessarily run the standard suites, a multi-config setup seems like if could be theoretically possible?
In 2nd place, having a single wallaby config to cover everything in
./amplify/backend/function
so that it could trigger all the lambda function tests at once. I attempted this with./wallaby.config.amplify.backend.js
Finally, one wallaby config per function would at least allow the reduced feedback loop when working on a single lambda function. This approach is in
./amplify/backend/function/amplifyFunction1/wallaby.config.js
.I've read most of the config pages on https://wallabyjs.com/docs/ but have been unable to get any of the above approaches working. I'm in danger of spending more time on setup than I would ever recover with faster feedback loops so I would appreciate some help / pointers for next steps.
Wallaby diagnostics report
./wallaby.config.amplify.backend.js
Diagnostics from `./wallaby.config.amplify.backend.js`
``` { editorVersion: '1.60.1', pluginVersion: '1.0.313', editorType: 'VSCode', osVersion: 'darwin 20.6.0', nodeVersion: 'v16.6.2', coreVersion: '1.0.1145', config: { files: [ { pattern: 'amplify/backend/function/**/*.{js,jsx,ts,tsx}', ignore: false, trigger: true, load: true, instrument: true, order: 1 }, { pattern: 'amplify/backend/function/**/*.{spec,test}.{js,jsx,ts,tsx}', ignore: true, trigger: true, load: true, instrument: true } ], tests: [ { pattern: 'amplify/backend/function/**/*.{spec,test}.{js,jsx,ts,tsx}', ignore: false, trigger: true, load: true, test: true, order: 2 } ], env: { type: 'node', params: {}, runner: '/usr/local/bin/node', viewportSize: { width: 800, height: 600 }, options: { width: 800, height: 600 }, bundle: true }, compilers: { '**/*.ts?(x)': [Function (anonymous)], '**/*.?(lit)coffee?(.md)': [Function (anonymous)] }, testRunner: { config: 'jest.config.amplify.backend.js' }, diagnostics: {}, filesWithNoCoverageCalculated: [], runAllTestsInAffectedTestFile: false, updateNoMoreThanOneSnapshotPerTestFileRun: false, addModifiedTestFileToExclusiveTestRun: true, preprocessors: {}, maxConsoleMessagesPerTest: 100, autoConsoleLog: true, delays: { run: 0, edit: 100, update: 0 }, workers: { initial: 0, regular: 0, recycle: false }, teardown: undefined, hints: { ignoreCoverage: '__REGEXP /ignore coverage|istanbul ignore/', ignoreCoverageForFile: '__REGEXP /ignore file coverage/', commentAutoLog: '?', testFileSelection: { include: '__REGEXP /file\\.only/', exclude: '__REGEXP /file\\.skip/' } }, automaticTestFileSelection: true, runSelectedTestsOnly: false, mapConsoleMessagesStackTrace: false, extensions: {}, reportUnhandledPromises: true, testFramework: { version: 'mocha@2.1.0', configurator: 'mocha@2.1.0', reporter: 'mocha@2.1.0', starter: 'mocha@2.1.0' }, slowTestThreshold: 75, lowCoverageThreshold: 80, loose: undefined, symlinkNodeModules: undefined, configCode: 'module.exports = function (wallaby) {\n' + ' return {\n' + ' files: [\n' + ' "amplify/backend/function/**/*.{js,jsx,ts,tsx}",\n' + ' {\n' + ' pattern: "amplify/backend/function/**/*.{spec,test}.{js,jsx,ts,tsx}",\n' + ' ignore: true,\n' + ' },\n' + ' ],\n' + ' tests: ["amplify/backend/function/**/*.{spec,test}.{js,jsx,ts,tsx}"],\n' + ' env: {\n' + ' type: "node",\n' + ' },\n' + ' compilers: {\n' + ' "**/*.ts?(x)": wallaby.compilers.typeScript({\n' + ' module: "commonjs",\n' + ' jsx: "React",\n' + ' isolatedModules: true,\n' + ' outDir: "./src",\n' + ' }),\n' + ' },\n' + ' testRunner: {\n' + ' config: "jest.config.amplify.backend.js"\n' + ' }\n' + ' };\n' + '};\n' }, packageJSON: { dependencies: { '@aws-amplify/ui-react': '^1.2.15', '@testing-library/jest-dom': '^5.14.1', '@testing-library/react': '^12.1.0', '@testing-library/user-event': '^13.2.1', 'aws-amplify': '^4.2.9', react: '^17.0.2', 'react-dom': '^17.0.2', 'react-scripts': '4.0.3', 'web-vitals': '^2.1.0' }, devDependencies: { '@types/jest': '^26.0.14', 'ts-jest': '^26.5.6', typescript: '^4.4.3' } }, fs: { numberOfFiles: 7 }, debug: [ '2021-09-19T23:08:27.027Z project Wallaby Node version: v16.6.2\n', '2021-09-19T23:08:27.027Z project Wallaby config:./amplify/backend/function/amplifyFunction1/wallaby.config.js
Diagnostics from `./amplify/backend/function/amplifyFunction1/wallaby.config.js`
``` { editorVersion: '1.60.1', pluginVersion: '1.0.313', editorType: 'VSCode', osVersion: 'darwin 20.6.0', nodeVersion: 'v16.6.2', coreVersion: '1.0.1145', config: { env: { type: 'node', params: {}, runner: '/usr/local/bin/node', viewportSize: { width: 800, height: 600 }, options: { width: 800, height: 600 }, bundle: true }, compilers: { '**/*.ts?(x)': [Function (anonymous)] }, testFramework: { version: 'jest@24.8.0', configurator: 'jest@24.8.0', reporter: 'jest@24.8.0', starter: 'jest@24.8.0', autoDetected: true }, diagnostics: { jest: { config: { configs: [ { automock: false, cache: true, cacheDirectory: '/private/var/folders/mv/xpp32cys7lnddv91021y1xpw0000gp/T/jest_dy', clearMocks: false, coveragePathIgnorePatterns: [ '/node_modules/' ], cwd: '