samedi 17 novembre 2018

Why is my Jest test passing even through I have an undefined variable in my JSX, instead of failing due to a Reference Error?

So I have purposely inserted an undefined variable in my JSX in the hope that the initial test fails. Unfortunately, the test passes. Why does this happen? The component is below.

import React, { Component} from "react";
import {hot} from "react-hot-loader";
import "./App.css";

class App extends Component{
    render(){
        return(
            <div className="App">
                <h1>{UndefinedVariable}</h1>
            </div>
        );
    }
}

export default hot(module)(App);

The test is below.

import React from "react";
import App from "./App";

test("renders without error", () => {

});

In a Udemy video (React Testing with Jest and Enzyme), this test failed. However, my test passed.

PASS  src/App.test.js (5.503s)
  ✓ renders without error (4ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        22.494s
Ran all test suites related to changed files.

Watch Usage: Press w to show more.

I have a feeling that this may be due to some error due to different versions but an online search came up short. These are my dependencies...

  "devDependencies": {
    "@babel/cli": "^7.1.0",
    "@babel/core": "^7.1.0",
    "@babel/preset-env": "^7.1.0",
    "@babel/preset-react": "^7.0.0",
    "babel-core": "^7.0.0-bridge.0",
    "babel-jest": "^23.6.0",
    "babel-loader": "^8.0.2",
    "css-loader": "^1.0.0",
    "eslint": "^5.9.0",
    "eslint-plugin-react": "^7.11.1",
    "identity-obj-proxy": "^3.0.0",
    "jest": "^23.6.0",
    "style-loader": "^0.23.0",
    "webpack": "^4.19.1",
    "webpack-cli": "^3.1.1",
    "webpack-dev-server": "^3.1.8"
  },
  "dependencies": {
    "react": "^16.5.2",
    "react-dom": "^16.5.2",
    "react-hot-loader": "^4.3.11"
  } 

This is my test script...

  "scripts": {
    "start": "webpack-dev-server --mode development",
    "build": "webpack --mode development",
    "test": "jest --watch"
  }

Any idea what is wrong here? Why is the test not failing due to a ReferenceError?

Aucun commentaire:

Enregistrer un commentaire