SalesforceCommerceCloud / pwa-kit

React-based JavaScript frontend framework to create a progressive web app (PWA) storefront for Salesforce B2C Commerce.
https://developer.salesforce.com/docs/commerce/pwa-kit-managed-runtime/guide/pwa-kit-overview.html
BSD 3-Clause "New" or "Revised" License
285 stars 133 forks source link

[BUG] Jest tests throw invalid parameter warning #1697

Closed AdamAnSubtractM closed 6 months ago

AdamAnSubtractM commented 7 months ago

Summary

Prior to "Extensibility", when running the Jest tests (npm run test), a warning would throw for each test stating the following:

 console.warn
    Invalid Parameter for getCustomerBaskets: clientId

      at node_modules/commerce-sdk-isomorphic/lib/index.cjs.js:1:182412
          at Array.forEach (<anonymous>)
      at e.<anonymous> (node_modules/commerce-sdk-isomorphic/lib/index.cjs.js:1:182297)
      at xa (node_modules/commerce-sdk-isomorphic/lib/index.cjs.js:1:22397)
      at Generator._invoke (node_modules/commerce-sdk-isomorphic/lib/index.cjs.js:1:22147)
      at Generator.forEach.e.<computed> [as next] (node_modules/commerce-sdk-isomorphic/lib/index.cjs.js:1:22771)
      at ca (node_modules/commerce-sdk-isomorphic/lib/index.cjs.js:1:20356)
      at o (node_modules/commerce-sdk-isomorphic/lib/index.cjs.js:1:20560)
      at node_modules/commerce-sdk-isomorphic/lib/index.cjs.js:1:20621
      at e.<anonymous> (node_modules/commerce-sdk-isomorphic/lib/index.cjs.js:1:20500)
      at e.<anonymous> (node_modules/commerce-sdk-isomorphic/lib/index.cjs.js:1:183195)
      at node_modules/@salesforce/commerce-sdk-react/hooks/ShopperCustomers/query.js:170:38
      at asyncGeneratorStep (node_modules/@salesforce/commerce-sdk-react/hooks/ShopperCustomers/query.js:14:103)
      at _next (node_modules/@salesforce/commerce-sdk-react/hooks/ShopperCustomers/query.js:15:194)
      at node_modules/@salesforce/commerce-sdk-react/hooks/ShopperCustomers/query.js:15:364
      at node_modules/@salesforce/commerce-sdk-react/hooks/ShopperCustomers/query.js:15:97
      at method (node_modules/@salesforce/commerce-sdk-react/hooks/ShopperCustomers/query.js:173:20)
      at node_modules/@salesforce/commerce-sdk-react/hooks/useAuthorizationHeader.js:34:20
      at asyncGeneratorStep (node_modules/@salesforce/commerce-sdk-react/hooks/useAuthorizationHeader.js:14:103)
      at _next (node_modules/@salesforce/commerce-sdk-react/hooks/useAuthorizationHeader.js:15:194)

  console.warn
    Invalid Parameter for getCustomerBaskets: locale

      at node_modules/commerce-sdk-isomorphic/lib/index.cjs.js:1:182412
          at Array.forEach (<anonymous>)
      at e.<anonymous> (node_modules/commerce-sdk-isomorphic/lib/index.cjs.js:1:182297)
      at xa (node_modules/commerce-sdk-isomorphic/lib/index.cjs.js:1:22397)
      at Generator._invoke (node_modules/commerce-sdk-isomorphic/lib/index.cjs.js:1:22147)
      at Generator.forEach.e.<computed> [as next] (node_modules/commerce-sdk-isomorphic/lib/index.cjs.js:1:22771)
      at ca (node_modules/commerce-sdk-isomorphic/lib/index.cjs.js:1:20356)
      at o (node_modules/commerce-sdk-isomorphic/lib/index.cjs.js:1:20560)
      at node_modules/commerce-sdk-isomorphic/lib/index.cjs.js:1:20621
      at e.<anonymous> (node_modules/commerce-sdk-isomorphic/lib/index.cjs.js:1:20500)
      at e.<anonymous> (node_modules/commerce-sdk-isomorphic/lib/index.cjs.js:1:183195)
      at node_modules/@salesforce/commerce-sdk-react/hooks/ShopperCustomers/query.js:170:38
      at asyncGeneratorStep (node_modules/@salesforce/commerce-sdk-react/hooks/ShopperCustomers/query.js:14:103)
      at _next (node_modules/@salesforce/commerce-sdk-react/hooks/ShopperCustomers/query.js:15:194)
      at node_modules/@salesforce/commerce-sdk-react/hooks/ShopperCustomers/query.js:15:364
      at node_modules/@salesforce/commerce-sdk-react/hooks/ShopperCustomers/query.js:15:97
      at method (node_modules/@salesforce/commerce-sdk-react/hooks/ShopperCustomers/query.js:173:20)
      at node_modules/@salesforce/commerce-sdk-react/hooks/useAuthorizationHeader.js:34:20
      at asyncGeneratorStep (node_modules/@salesforce/commerce-sdk-react/hooks/useAuthorizationHeader.js:14:103)
      at _next (node_modules/@salesforce/commerce-sdk-react/hooks/useAuthorizationHeader.js:15:194)

Now with "Extensibility" in place, running npm run test from our repo does not run the tests on the retail-react-app components as those are now in a package, however, if you implement Jest testing by extending the @salesforce/retail-react-app jest and babel configs, you getting this warning for each component you have tests set up for.

Steps To Reproduce

Reproducing prior to "Extensibility"

  1. Download pwa-kit-3.3.0 from here
  2. Extract the zip (doing it this way will not have "Extensibility" in place)
  3. Make sure your node version is 18 or late and npm version is 9 or later
  4. Via the CLI, in the root of the pwa-kit-3.3.0 directory you extracted, type: cd packages/template-retail-react-app && npm i && npm run test
  5. Note the warnings above thrown v3 3 0

Reproducing with the latest version of pwa-kit-create-app

  1. Make sure your node version is 18 or late and npm version is 9 or later
  2. Via the CLI, run type: npx @salesforce/pwa-kit-create-app --preset "retail-react-app"
  3. When prompted with "Do you wish to use template extensibility?", select "yes"
  4. Go through the prompts fill out the rest of your commerce cloud details
  5. Once complete with the prompts, this will install the dependencies of the project for you. Switch to that project root: (i.e cd retail-react-app)
  6. Modify babel.config.js (this will allow jest to use the right runtime. No need to install extra dependencies)
    
    const base = require('@salesforce/pwa-kit-dev/configs/babel/babel-config')

const configs = { // base config from Salesforce PWA Kit ...base, default: { ...base.default, env: { ...base.default.env, test: { ...base.default.env.test, presets: ['@babel/preset-env', ['@babel/preset-react', {runtime: 'automatic'}]], plugins: ['babel-plugin-dynamic-import-node-babel-7'] } } } } module.exports = configs.default

7. Create a `jest.config.js` in the root of the project. Extend the default config to look like this:

const base = require('@salesforce/retail-react-app/jest.config.js') const path = require('path')

// extend/override config below const configs = { // base config from Salesforce PWA Kit ...base, collectCoverage: false, collectCoverageFrom: [ ...base.collectCoverageFrom, 'app//*.{js,jsx,ts,tsx}', 'non-pwa/*/.{js,jsx,ts,tsx}', '!worker//.{js,jsx,ts,tsx}', 'scripts/generator/.{js,jsx,ts,tsx}', '!app/pages/test-container/*/.{js,jsx,ts,tsx}', '!app/types/*' ], // maps aliases for Jest to understand // if you have any special aliases for your directories, include them in moduleNameMapper moduleNameMapper: { ...base.moduleNameMapper, '^@salesforce/retail-react-app(.)$': '/node_modules/@salesforce/retail-react-app/$1', }, roots: [''], // directories jest will ignore testPathIgnorePatterns: [...base.testPathIgnorePatterns, 'dist'], // so we can use ESM with Jest as Jest is built on commonjs transform: { '\.[jt]sx?$': ['babel-jest', {configFile: path.resolve(__dirname, 'babel.config.js')}] }, transformIgnorePatterns: [] }

module.exports = configs


8. In the `package.json` update the "test" script to to look like this `"test": "jest --passWithNoTests",`
9. Create a basic Jest test (I recommend creating a shell component and write a test to expect it to be in the document OR grab one from the "retail-react-app" package
10. Via the CLI, run: `npm run test`
11. Observe, you get the same warning as above

![Screenshot 2024-03-13 at 12 33 26 PM](https://github.com/SalesforceCommerceCloud/pwa-kit/assets/13452469/24d3791d-88dd-4753-babc-eb0491739b6f)
![Screenshot 2024-03-13 at 12 33 45 PM](https://github.com/SalesforceCommerceCloud/pwa-kit/assets/13452469/e76234bd-66b4-4a38-aeab-c7d9a32f3e2e)

### Expected result

- Test should pass/fail without warnings of Invalid Parameters

### Actual result

- Tests are passing / failing and there are warnings being thrown about Invalid Parameters

### System Information (as applicable)

**Browser**:
N/A
**Node version**:
v18.19.1
**pwa-kit version**:

- "@salesforce/retail-react-app": "2.2.0"
- "@salesforce/pwa-kit-dev": "3.3.0"

**Desktop OS**:
macOS 14.4 (23E214)
**Mobile Device Info**:
N/A

### Additional information

- I suspect this has something to do with the API mocking that is being done. Something in that mocking is passing an invalid parameter to those function mocks or those functions are not typed to expect those parameters
joeluong-sfcc commented 7 months ago

Hey @AdamAnSubtractM, thanks for creating this issue. For context, the PWA Kit makes SCAPI calls through the commerce-sdk-isomorphic, which is essentially a javascript library that makes calling SCAPI easy. Previously with the package, if you passed in an invalid query parameter, it would be filtered out under the hood without the user knowing. We recently added support for custom query parameters to the commerce-sdk-isomorphic package, and as part of that change, we explicitly notify the user if an invalid query param is passed with a warning.

The PWA Kit previously passed on clientId, currency, and locale to all SDK calls, which are invalid query parameters for certain endpoints, but that was resolved with this PR: https://github.com/SalesforceCommerceCloud/pwa-kit/pull/1655. That change hasn't been released yet, but once it is, those warnings should disappear. The warnings are more of a nuisance than originally anticipated, so moving forward we'll consider either removing this warning entirely or potentially introducing a toggle switch for disabling warnings.

tl;dr: Once the new version of @salesforce/commerce-sdk-react gets released, these warnings no longer appear

joeluong-sfcc commented 6 months ago

Closing as this should be resolved with the latest release of @salesforce/commerce-sdk-react@1.4.2