Gatsby: Instantiating an instance of Link in unit tests triggers an error in withPrefix

Created on 4 Jun 2020  路  2Comments  路  Source: gatsbyjs/gatsby

Description

Running unit tests in my project (jest and react testing library) triggers a failure in the Link component in the withPrefix function.

The specific issue is the following line (which was recently changed):

export function withPrefix(path, prefix = __BASE_PATH__) {

If __BASE_PATH__ is not defined, this triggers an error. The previous version of this file guarded against this case by checking that typeof __BASE_PATH__ is defined.

I will be submitting a PR shortly with a fix.

Steps to reproduce

Run a unit test using Jest with RTL for any component that uses the Gatsby Link component.

Expected result

No reference error.

Actual result

The specific error is:

ReferenceError: __BASE_PATH__ is not defined

Environment

  System:
    OS: macOS 10.15.4
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 10.21.0 - /var/folders/82/v_28mld95zqd97n81x_c2kb00000gn/T/yarn--1591305860413-0.03955211442214068/node
    Yarn: 1.22.4 - /var/folders/82/v_28mld95zqd97n81x_c2kb00000gn/T/yarn--1591305860413-0.03955211442214068/yarn
    npm: 6.14.4 - /usr/local/bin/npm
  Languages:
    Python: 2.7.16 - /usr/bin/python
  Browsers:
    Chrome: 83.0.4103.97
    Edge: 83.0.478.44
    Firefox: 77.0
    Safari: 13.1
  npmPackages:
    gatsby: ^2.23.0 => 2.23.0
    gatsby-background-image: ^1.0.1 => 1.1.1
    gatsby-cli: ^2.12.43 => 2.12.43
    gatsby-image: ^2.4.6 => 2.4.6
    gatsby-plugin-google-analytics: ^2.3.3 => 2.3.3
    gatsby-plugin-google-tagmanager: ^2.3.4 => 2.3.4
    gatsby-plugin-lodash: ^3.3.3 => 3.3.3
    gatsby-plugin-module-resolver: ^1.0.3 => 1.0.3
    gatsby-plugin-prefetch-google-fonts: ^1.4.3 => 1.4.3
    gatsby-plugin-react-helmet: ^3.3.3 => 3.3.3
    gatsby-plugin-react-svg: ^3.0.0 => 3.0.0
    gatsby-plugin-remove-generator: ^1.0.5 => 1.0.5
    gatsby-plugin-remove-trailing-slashes: ^2.3.4 => 2.3.4
    gatsby-plugin-root-import: ^2.0.5 => 2.0.5
    gatsby-plugin-sass: ^2.3.3 => 2.3.3
    gatsby-plugin-schema-snapshot: ^1.0.0 => 1.0.0
    gatsby-plugin-sharp: ^2.6.10 => 2.6.10
    gatsby-plugin-sitemap: ./plugins/gatsby-plugin-sitemap/ => 2.2.4
    gatsby-plugin-styled-components: ^3.3.3 => 3.3.3
    gatsby-plugin-typescript: ^2.4.4 => 2.4.4
    gatsby-plugin-webpack-bundle-analyser-v2: ^1.1.10 => 1.1.10
    gatsby-source-contentful: 2.3.14 => 2.3.14
    gatsby-source-filesystem: ^2.3.10 => 2.3.10
    gatsby-transformer-json: ^2.4.4 => 2.4.4
    gatsby-transformer-sharp: ^2.5.4 => 2.5.4
    gatsby-transformer-sqip: ^2.3.10 => 2.3.10
inkteam assigned reacrouter and Links bug

Most helpful comment

As workaround, you can configure jest.config.js

globals: {
  __PATH_PREFIX__: ``,
  __BASE_PATH__: ``, // add this line
},

see Unit testing docs for more info.

All 2 comments

Here is the PR with the fix: https://github.com/gatsbyjs/gatsby/pull/24790

As workaround, you can configure jest.config.js

globals: {
  __PATH_PREFIX__: ``,
  __BASE_PATH__: ``, // add this line
},

see Unit testing docs for more info.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

3CordGuy picture 3CordGuy  路  3Comments

mikestopcontinues picture mikestopcontinues  路  3Comments

signalwerk picture signalwerk  路  3Comments

magicly picture magicly  路  3Comments

benstr picture benstr  路  3Comments