TypeError:无法读取未定义的属性“cwd”
Posted
技术标签:
【中文标题】TypeError:无法读取未定义的属性“cwd”【英文标题】:TypeError: Cannot read property 'cwd' of undefined 【发布时间】:2019-07-09 12:16:21 【问题描述】:我正在使用 jest 和 react-testing-library 编写测试用例 当我运行我的测试脚本时,它会引发如下错误
TypeError: Cannot read property 'cwd' of undefined
现在我已经尝试了几乎所有方法来解决这个问题,但没有找到原因
我尝试更改配置,即使我已经使用 --no-cache 运行,但它再次抛出 同样的错误
这是我的配置文件
const defaults = require("jest-config");
module.exports =
testEnvironment: "node",
// setupFiles: [
// "<rootDir>/../apollo-server-env/dist/index.js"
// ],
preset: "ts-jest",
testMatch: null,
testRegex: ".*test*\\.(ts|tsx|js)$",
testPathIgnorePatterns: [
"/node_modules/",
"/dist/"
],
transform:
"\\.(gql|graphql)$": "jest-transform-graphql",
"\\.(ts|tsx)$": "ts-jest",
// Use our custom transformer only for the *.js and *.jsx files
"\\.(js|jsx)?$": "./transform.js",
// future need to test with
// "^.+\\.(js|jsx|ts|tsx)$": "./transform.js",
// ".+\\.(css|styl|less|sass|scss)$": "jest-css-modules-transform"
,
roots: [
"packages",
"packages-modules",
"servers"
],
moduleFileExtensions: [...defaults.moduleFileExtensions,
"ts",
"tsx",
"js",
"gql",
"graphql"],
moduleNameMapper:
'^__mocks__/(.*)$': '<rootDir>/../../__mocks__/$1',
// This regex should match the packages that we want compiled from source
// through `ts-jest`, as opposed to loaded from their output files in
// `dist`.
// We don't want to match `apollo-server-env` and
// `apollo-engine-reporting-protobuf`, because these don't depend on
// compilation but need to be initialized from as parto of `prepare`.
'^(?!apollo-server-env|apollo-engine-reporting-protobuf)(apollo-(?:server|datasource|cache-control|tracing|engine)[^/]*|graphql-extensions)(?:/dist)?((?:/.*)|$)': '<rootDir>/../../packages/$1/src$2'
,
transformIgnorePatterns: [
"/node_modules/(?!(@vscode)/).*/"
],
clearMocks: true,
globals:
__BACKEND_URL__: 'http://localhost:3010',
__GRAPHQL_URL__: 'http://localhost:8085/graphql',
/*"ts-jest":
tsConfig: "<rootDir>/tsconfig.json",
//https://github.com/kulshekhar/ts-jest/issues/766,
"diagnostics":
"warnOnly": true
,
"babelConfig": true
*/
;```
Here is my test file
`import * as React from 'react';
import render, cleanup from 'react-testing-library';
import EditorComponent from '../components/editor/editor-component';
import Provider from 'react-fela';
import createRenderer from 'fela';
import 'jest';
import 'jest-dom/extend-expect';
import MockedProvider from 'react-apollo/test-utils';
import FILE_CONTENT_QUERY from '../../../files-graphql-client/src/queries/index';
const mocks = [
request:
query : FILE_CONTENT_QUERY,
,
result:
data:
'getReleaseNotes': ,
,
,
];
describe('<EditorComponent/>', () =>
let componentObj, felaRenderer, props =
subscriber: jest.fn(),
saveDelayFileChangesMutation: jest.fn(),
loadFileContent: function ()
return new Promise(function(resolve)
setTimeout(function()
resolve(data: loadFileContent:
'loadFileContent': ,
);
, 300);
);
,
openingFiles: [
value : 'test',
encoding: 'utf-8',
oldValue : 'oldTest',
deleted : false,
addIgnore : 'test',
editorId : 1,
markdown: false,
preview : true,
preOpen : false,
keepOpen : true,
status : 'test',
oversize : false,
changed : true,
changedEvent: ,
diff : true,
size : 25,
] as IContent[],
allSave: false,
rootPath: '',
change: jest.fn(),
actions: [],
doneSaveAll: jest.fn(),
changeEditorSubscribes: jest.fn(),
updateEditorContent: jest.fn(),
openDiffEditor: jest.fn(),
closeConflicts: jest.fn(),
closeEditor: jest.fn(),
updateChangedContent: jest.fn(),
setFinderOptions: jest.fn(),
onOpenFile: jest.fn(),
styles: ,
subscribeToMore: jest.fn(),
;
beforeEach(() =>
felaRenderer = createRenderer();
componentObj = render(<MockedProvider mocks=mocks><Provider renderer=felaRenderer><EditorComponent ...props/></Provider></MockedProvider>);
);
afterEach(cleanup);
);`
【问题讨论】:
通常从错误信息中获取错误所在的文件名和行号。当您没有显示任何尝试访问cwd
的代码时,很难猜测发生了什么。因此,也许您可以发布完整的错误消息并发布引发错误的代码部分。
【参考方案1】:
我遇到了同样的问题,原因是babel-jest
已经升级到最新版本,而没有升级其余与 jest 相关的模块:
"babel-jest": "^26.6.3",
"jest": "^23.6.0",
在我的情况下,将 jest
升级到最新版本解决了这个问题:
"babel-jest": "^26.6.3",
"jest": "^26.6.3",
【讨论】:
【参考方案2】:只需安装旧版本的 babel-jest:22。 无需更改 jest 的版本,可以使用最新版本。 最新版本的 babel-jest 会产生问题
npm install --save-dev babel-jest@22.4.3
【讨论】:
【参考方案3】:问题可能出在“babel-jest”模块的版本上。尝试使用“jest”模块将其连接起来。 这些是我在 package.json 中的依赖项: ... "babel-jest": "^23", “笑话”:“22.4.3”, ...
【讨论】:
以上是关于TypeError:无法读取未定义的属性“cwd”的主要内容,如果未能解决你的问题,请参考以下文章