【问题标题】:How do I run my mocha.js tests in visual studio online build process如何在 Visual Studio 在线构建过程中运行我的 mocha.js 测试
【发布时间】:2017-04-04 09:24:17
【问题描述】:

我有一个使用 webpack 的 react web 应用程序。

由于 Left-pad 的问题,我将我的 node_modules 文件夹包含在源代码管理中。

我设置了 mocha 和酶来对我的反应模块进行单元测试,并且我的 packages.json 具有以下测试配置。

 "test": "mocha-webpack --webpack-config webpack.config-test.js \"source/**/*.test.js\""

这一切都很好。当我跑步时

npm run test

在我的本地开发机器上。我所有的测试都被执行了。

我遇到的问题是,当我在专用构建机器上运行“npm run test”作为构建过程的一部分时,出现以下错误

Error: Cannot find module 'enzyme'

经过一番挖掘,我发现如果我跑了

npm install enzyme

在我运行测试之前在构建机器上,然后它工作正常。 这样做的原因是安装修改了我的 node_modules/enzyme/packages.json 文件。此文件中存储了 2 个完整路径,其中包含指向我的本地开发环境的路径,并且它们已更改为我的项目在构建机器上运行的完整路径。

_Args 下一个,_where 下一个

   {
  "_args": [
    [
      {
        "raw": "enzyme@^2.6.0",
        "scope": null,
        "escapedName": "enzyme",
        "name": "enzyme",
        "rawSpec": "^2.6.0",
        "spec": ">=2.6.0 <3.0.0",
        "type": "range"
      },
      "C:\\Dev\\UnifiedWeb"
    ]
  ],
  "_from": "enzyme@>=2.6.0 <3.0.0",
  "_id": "enzyme@2.6.0",
  "_inCache": true,
  "_location": "/enzyme",
  "_nodeVersion": "7.0.0",
  "_npmOperationalInternal": {
    "host": "packages-12-west.internal.npmjs.com",
    "tmp": "tmp/enzyme-2.6.0.tgz_1478746866117_0.454174768878147"
  },
  "_npmUser": {
    "name": "ljharb",
    "email": "ljharb@gmail.com"
  },
  "_npmVersion": "3.10.8",
  "_phantomChildren": {},
  "_requested": {
    "raw": "enzyme@^2.6.0",
    "scope": null,
    "escapedName": "enzyme",
    "name": "enzyme",
    "rawSpec": "^2.6.0",
    "spec": ">=2.6.0 <3.0.0",
    "type": "range"
  },
  "_requiredBy": [
    "#DEV:/",
    "#USER"
  ],
  "_resolved": "https://registry.npmjs.org/enzyme/-/enzyme-2.6.0.tgz",
  "_shasum": "148d742b25e2565f7e80870a0c92aea9be1b90ea",
  "_shrinkwrap": null,
  "_spec": "enzyme@^2.6.0",
  "_where": "C:\\Dev\\UnifiedWeb",
  "author": {
    "name": "Leland Richardson",
    "email": "leland.richardson@airbnb.com"
  },
  "bugs": {
    "url": "https://github.com/airbnb/enzyme/issues"
  },
  "dependencies": {
    "cheerio": "^0.22.0",
    "function.prototype.name": "^1.0.0",
    "in-publish": "^2.0.0",
    "is-subset": "^0.1.1",
    "lodash": "^4.16.4",
    "object-is": "^1.0.1",
    "object.assign": "^4.0.4",
    "object.entries": "^1.0.3",
    "object.values": "^1.0.3",
    "uuid": "^2.0.3"
  },
  "description": "JavaScript Testing utilities for React",
  "devDependencies": {
    "babel-cli": "^6.18.0",
    "babel-core": "^6.18.2",
    "babel-eslint": "^7.1.0",
    "babel-loader": "^6.2.7",
    "babel-preset-airbnb": "^2.1.1",
    "babel-register": "^6.18.0",
    "chai": "^3.5.0",
    "coveralls": "^2.11.14",
    "enzyme-example-jest": "^0.1.0",
    "enzyme-example-karma": "^0.1.1",
    "enzyme-example-karma-webpack": "^0.1.4",
    "enzyme-example-mocha": "^0.1.0",
    "enzyme-example-react-native": "^0.1.0",
    "eslint": "^3.9.1",
    "eslint-config-airbnb": "^13.0.0",
    "eslint-plugin-import": "^2.2.0",
    "eslint-plugin-jsx-a11y": "^2.2.3",
    "eslint-plugin-react": "^6.6.0",
    "gitbook-cli": "^1.0.1",
    "istanbul": "^1.0.0-alpha.2",
    "jsdom": "^6.1.0",
    "json-loader": "^0.5.4",
    "karma": "^1.3.0",
    "karma-chrome-launcher": "^1.0.1",
    "karma-firefox-launcher": "^1.0.0",
    "karma-mocha": "^1.2.0",
    "karma-sourcemap-loader": "^0.3.7",
    "karma-webpack": "^1.8.0",
    "mocha": "^3.1.2",
    "rimraf": "^2.5.4",
    "safe-publish-latest": "^1.1.1",
    "sinon": "^1.17.6",
    "webpack": "^1.13.3"
  },
  "directories": {},
  "dist": {
    "shasum": "148d742b25e2565f7e80870a0c92aea9be1b90ea",
    "tarball": "https://registry.npmjs.org/enzyme/-/enzyme-2.6.0.tgz"
  },
  "gitHead": "5c37d91715a88d393f3c260447a189a446d76e0c",
  "homepage": "https://github.com/airbnb/enzyme#readme",
  "keywords": [
    "javascript",
    "shallow rendering",
    "shallowRender",
    "test",
    "reactjs",
    "react",
    "flux",
    "testing",
    "test utils",
    "assertion helpers",
    "tdd",
    "mocha"
  ],
  "license": "MIT",
  "main": "build",
  "maintainers": [
    {
      "name": "airbnb",
      "email": "jordan.harband+npm@airbnb.com"
    },
    {
      "name": "intelligibabble",
      "email": "leland.m.richardson@gmail.com"
    },
    {
      "name": "ljharb",
      "email": "ljharb@gmail.com"
    }
  ],
  "name": "enzyme",
  "optionalDependencies": {},
  "peerDependencies": {
    "react": "0.13.x || 0.14.x || ^15.0.0-0 || 15.x"
  },
  "readme": "ERROR: No README data found!",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/airbnb/enzyme.git"
  },
  "scripts": {
    "build": "babel src --out-dir build",
    "check": "npm run lint && npm run test:all",
    "clean": "rimraf build",
    "docs:build": "npm run docs:prepare && gitbook build",
    "docs:clean": "rimraf _book",
    "docs:prepare": "gitbook install",
    "docs:publish": "npm run docs:clean && npm run docs:build && cd _book && git init && git commit --allow-empty -m 'update book' && git fetch git@github.com:airbnb/enzyme.git gh-pages && git checkout -b gh-pages && git add . && git commit -am 'update book' && git push git@github.com:airbnb/enzyme.git gh-pages --force",
    "docs:watch": "npm run docs:prepare && gitbook serve",
    "lint": "eslint --ext js,jsx src test",
    "postversion": "git push && git push --tags && npm run clean && npm run docs:publish",
    "prepublish": "not-in-publish || (npm run clean && npm run build && safe-publish-latest)",
    "pretest": "npm run lint",
    "preversion": "npm run clean && npm run check",
    "react:13": "npm run react:clean && npm i react@0.13",
    "react:14": "npm run react:clean && npm i react@0.14 react-dom@0.14 react-addons-test-utils@0.14",
    "react:15": "npm run react:clean && npm i react@15 react-dom@15 react-addons-test-utils@15",
    "react:clean": "rimraf node_modules/react node_modules/react-dom node_modules/react-addons-test-utils",
    "test": "npm run clean && npm run build && npm run test:only",
    "test:all": "npm run react:13 && npm run test:only && npm run react:14 && npm run test:only && npm run react:15 && npm run test:only",
    "test:env": "sh ./example-test.sh",
    "test:karma": "karma start",
    "test:only": "mocha --recursive test",
    "test:single": "mocha --watch",
    "test:watch": "mocha --recursive --watch test",
    "travis": "babel-node ./node_modules/.bin/istanbul cover --report html _mocha -- test --recursive",
    "version": "npm run build"
  },
  "version": "2.6.0"
}

这个问题也会影响我团队中的其他开发人员,因为如果他们想在本地运行测试,则需要将路径更改为他们的本地开发。

为什么酶包里是全路径?有没有更好的方法来处理这个?是我遗漏了什么,还是 npm 不可移植?

【问题讨论】:

  • 您是否偶然使用了npm shrinkwrap?查看enzyme package.json 文件,它似乎没有本地路径。
  • 谢谢马可。我刚刚添加了我的酵素/package.json。我认为 NPM 添加它们而不是酶

标签: reactjs npm webpack mocha.js enzyme


【解决方案1】:

你试过npm link ext/* --prefix=node_modules

根据这个线程 - Local dependency in package.json

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-02
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多