NuxtJS - Testing

A collection of relevant information to make testing NuxtJS sites more efficient.



CONFIGURATIONS

In a default NuxtJS project, ensure the following configurations are present.

In file ~/jest.config.js:

NOTE! the addition of 'gql' to ensure that Apollo GraphQL files in ~/apollo/queries/* don't interfere with the Jest functionality, and especially with VS Code Jest extension.

module.exports = {
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/$1',
    '^~/(.*)$': '<rootDir>/$1',
    '^vue$': 'vue/dist/vue.common.js'
  },
  moduleFileExtensions: ['js', 'vue', 'json', 'gql'],
  transform: {
    '^.+\\.js$': 'babel-jest',
    '.*\\.(vue)$': 'vue-jest'
  }
}
1
2
3
4
5
6
7
8
9
10
11
12

In ~/.vscode/settings.json file, ensure the following is present:

{
  "jest.pathToJest": "yarn run test",
  "jest.showCoverageOnLoad": true
}
1
2
3
4

In ~/package.json file, ensure the following is present:

"devDependencies": {
  <snip>
  "jest": "^XX.X.X",
  "jest-cli": "^XX.X.X",
  <snip>
}
1
2
3
4
5
6