question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unmocking axios? I get 'the string did not match the expected pattern'

See original GitHub issue

OS: windows 10 Node version: 8.9.1 Jest version: 22.0.4

issue: with the following code:

jest.unmock('axios');
import axios from "axios";

function parseJson(data) { return data ? JSON.parse(data) : null; }

function testMe () {

    return axios.get('/api/data/product-information', {
        auth: {
            username: 'admin',
            password: 'root'
        },
        headers: {
            "Accept": "application/yang-data+json",
            "Cache-Control": "no-store, no-cache, must-revalidate"
        },
        withCredentials: true,
        transformRequest: parseJson
    })
}


test('test me', () => {
    testMe().then((data) => {
        console.log(data);
    }).catch(err => {
        console.log(err);
    });

    expect(1).toBe(1)
});

I get the following in return:

> polatis-app-test@1.0.0 test C:\Users\sheppqu\WebstormProjects\polatis files\polatis\polatis-app
> jest --config jest.config.js --no-cache

 PASS  __tests__\restapi.test.js
  √ test get_connection method returns expected properties (5ms)

  console.log __tests__\restapi.test.js:28
    SyntaxError: The string did not match the expected pattern.
        at XMLHttpRequest.open (C:\Users\sheppqu\WebstormProjects\polatis files\polatis\polatis-app\node_modules\jsdom\
lib\jsdom\living\xmlhttprequest.js:470:15)
        at dispatchXhrRequest (C:\Users\sheppqu\WebstormProjects\polatis files\polatis\polatis-app\node_modules\axios\l
ib\adapters\xhr.js:45:13)
        at new Promise (<anonymous>)
        at xhrAdapter (C:\Users\sheppqu\WebstormProjects\polatis files\polatis\polatis-app\node_modules\axios\lib\adapt
ers\xhr.js:12:10)
        at dispatchRequest (C:\Users\sheppqu\WebstormProjects\polatis files\polatis\polatis-app\node_modules\axios\lib\
core\dispatchRequest.js:59:10)
        at <anonymous>
        at process._tickCallback (internal/process/next_tick.js:188:7)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        2.432s
Ran all test suites.

Note the syntax error, SyntaxError: The string did not match the expected pattern.. What is going on? I need to be able to run an unmocked test using axios to test the data returned from the end points that are (will be) in a seperate library.

as addition these are my setup files:

.babelrc

{
  "presets": [
    ["env",
      {
        "targets": {
          "node": "current"
        }
      }],
    "jest"
  ],
  "plugins": ["transform-async-to-generator"]
}

jest.config.js

module.exports = {
    setupFiles: ['./jest.setup.js'],
    snapshotSerializers: ['enzyme-to-json/serializer']
};

package.json

{
  "name": "polatis-app-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "rimraf ./dist && webpack-dev-server --config webpack.dev.config.js",
    "prod": "rimraf ./dist && webpack --config webpack.production.config.js",
    "test": "jest --config jest.config.js --no-cache"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.0.0-beta.39",
    "@babel/preset-env": "^7.0.0-beta.39",
    "autoprefixer": "^7.2.2",
    "babel": "^6.23.0",
    "babel-core": "^6.26.0",
    "babel-jest": "^22.0.4",
    "babel-loader": "^7.1.2",
    "babel-plugin-react-intl": "^2.3.1",
    "babel-plugin-transform-async-to-generator": "^6.24.1",
    "babel-plugin-transform-es2015-block-scoping": "^6.26.0",
    "babel-plugin-transform-es2015-constants": "^6.1.4",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-es2015-ie": "^6.7.0",
    "babel-preset-es2017": "^6.24.1",
    "babel-preset-es3": "^1.0.1",
    "babel-preset-latest": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "babel-preset-stage-1": "^6.24.1",
    "babel-preset-stage-3": "^6.24.1",
    "copy-webpack-plugin": "^4.2.3",
    "core-js": "^2.5.3",
    "css-loader": "^0.28.7",
    "enzyme": "^3.3.0",
    "enzyme-adapter-react-16": "^1.1.1",
    "enzyme-to-json": "^3.3.0",
    "extract-text-webpack-plugin": "^3.0.2",
    "html-webpack-plugin": "^2.30.1",
    "jest": "^22.0.4",
    "jest-cli": "^22.0.4",
    "node-sass": "^4.7.2",
    "postcss": "^6.0.14",
    "postcss-cssnext": "^3.0.2",
    "postcss-flexbugs-fixes": "^3.2.0",
    "postcss-import": "^11.0.0",
    "postcss-loader": "^2.0.9",
    "raf": "^3.4.0",
    "react-hot-loader": "^3.1.3",
    "react-test-renderer": "^16.2.0",
    "regenerator-runtime": "^0.11.1",
    "rimraf": "^2.6.2",
    "sass-loader": "^6.0.6",
    "style-loader": "^0.19.0",
    "url-loader": "^0.6.2",
    "webpack": "^3.10.0",
    "webpack-dev-server": "^2.9.7"
  },
  "dependencies": {
    "axios": "^0.18.0",
    "babel-runtime": "^6.26.0",
    "bootstrap": "^3.3.7",
    "classlist-polyfill": "^1.2.0",
    "es5-shim": "^4.5.10",
    "file-loader": "^1.1.6",
    "history": "^4.7.2",
    "i18n-react": "^0.6.3",
    "intl": "^1.2.5",
    "js-cookie": "^2.2.0",
    "prop-types": "^15.6.0",
    "react": "^16.2.0",
    "react-bootstrap": "^0.32.1",
    "react-dom": "^16.2.0",
    "react-intl": "^2.4.0",
    "react-redux": "^5.0.6",
    "react-router": "^4.2.0",
    "react-router-bootstrap": "^0.24.4",
    "react-router-dom": "^4.2.2",
    "react-router-redux": "^5.0.0-alpha.9",
    "redux": "^3.7.2",
    "redux-logger": "^3.0.6",
    "redux-thunk": "^2.2.0",
    "save-dev": "^2.0.0"
  }
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:8

github_iconTop GitHub Comments

36reactions
SimenBcommented, Mar 7, 2018

Again, this is not a help forum. Please see the docs (https://facebook.github.io/jest/docs/en/configuration.html#testenvironment-string)

1reaction
sebworkscommented, Apr 27, 2018

Make sure you are setting a testURL in your Jest config. The error is typically generated by jsdom when the URL is invalid.

Read more comments on GitHub >

github_iconTop Results From Across the Web

I keep getting the error "the string did not match the expected ...
You may get this error if the server's response is text but you attempt to parse it as JSON using res.json() .
Read more >
[jest] SyntaxError: The string did not match the expected pattern
参考. detect jest and use http adapter instead of XMLhttpRequest · Issue #1180 · axios/axios · Unmocking axios? I get 'the string did...
Read more >
axios save cookie - You.com | The Search Engine You Control
I have searched around and I know that axios has a bug with cross site requests. ... I get 'the string did not...
Read more >
Great Expectations - Unmock
Unmock spies have some useful functions for this that follow a basic convention ... An exception is thrown if multiple calls matching the...
Read more >
API Docs | fetch-mock - Wheresrhys
If the sendAsJson option is set to true , any object that does not meet the criteria above will be converted to a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found