npm ERR! Missing script: "test" when running npm test in a ASP.Net Core React app

Viewed 131

I have set up a ASP.Net Core React app using dotnet new react like this person did. I've also added a Node console application containing a unit test file like the answer to the question suggested. The test file looks like this:

describe('Test Suite 1', function () {
    it("Test 1 - This shouldn't fail", function () {
        expect(true).toBeTruthy();
    });

    it('Test 2 - This should fail', function () {
        expect(1 === 1).toBeTruthy(); // "This shouldn't fail"
        expect(false).toBeTruthy();
    });
});

I can now try to run the tests via the following methods:

  • via developer command prompt (vstest.console.exe "G:\....\WebApp.UnitTest.njsproj" "/TestAdapterPath:E:\Program Files\Microsoft Visual Studio\2022\Comunity\Common7\IDE\Extensions\Microsoft\NodeJsTools\TestAdapter"). This runs the unit tests fine and says that Test 1 succeeds and Test 2 fails.
  • via the VS test explorer. Both tests are displayed and both fail. They also fail if I remove all code within them.
  • via the console using npm test. I get npm : npm ERR! Missing script: "test".

I have seen this answer to a similar problem, but since I don't have a jest.config I don't know how to apply it, and judging from the developer command prompt method, I also don't seem to need one?

My package.json in the react project looks like this:

{
  "name": "multiusercrossword",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "bootstrap": "^5.1.3",
    "http-proxy-middleware": "^0.19.1",
    "jquery": "^3.6.0",
    "merge": "^2.1.1",
    "oidc-client": "^1.11.5",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-router-bootstrap": "^0.25.0",
    "react-router-dom": "^5.2.0",
    "react-scripts": "^5.0.0",
    "reactstrap": "^8.9.0",
    "rimraf": "^2.6.2",
    "web-vitals": "^0.2.4",
    "workbox-background-sync": "^5.1.3",
    "workbox-broadcast-update": "^5.1.3",
    "workbox-cacheable-response": "^5.1.3",
    "workbox-core": "^5.1.3",
    "workbox-expiration": "^5.1.3",
    "workbox-google-analytics": "^5.1.3",
    "workbox-navigation-preload": "^5.1.3",
    "workbox-precaching": "^5.1.3",
    "workbox-range-requests": "^5.1.3",
    "workbox-routing": "^5.1.3",
    "workbox-strategies": "^5.1.3",
    "workbox-streams": "^5.1.3"
  },
  "devDependencies": {
    "ajv": "^6.9.1",
    "cross-env": "^7.0.3",
    "eslint": "^7.25.0",
    "eslint-config-react-app": "^6.0.0",
    "eslint-plugin-flowtype": "^5.7.2",
    "eslint-plugin-import": "^2.22.1",
    "eslint-plugin-jsx-a11y": "^6.4.1",
    "eslint-plugin-react": "^7.23.2",
    "nan": "^2.14.2",
    "typescript": "^4.2.4"
  },
  "scripts": {
    "prestart": "node aspnetcore-https && node aspnetcore-react",
    "start": "rimraf ./build && react-scripts start",
    "build": "react-scripts build",
    "test": "cross-env CI=true react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "lint": "eslint ./src/"
  },
  "eslintConfig": {
    "extends": [
      "react-app"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

and in the test project it looks like this:

{
  "name": "web-app.unit-test",
  "version": "0.0.0",
  "description": "WebApp.UnitTest",
  "main": "app.js",
  "author": {
    "name": ""
  },
  "scripts": {
    "test": "jest"
  },
  "devDependencies": {
    "jest": "^28.1.1",
    "jest-editor-support": "^30.1.0"
  }
}

How can I make my tests not fail in the test explorer? Alternatively, how can I solve the issue of Missing script: "test"?

0 Answers
Related