首页 > 解决方案 > Gitlab CI 打破单元测试

问题描述

我的两个测试在本地通过,但在 GitlabCI 上没有通过,对于我来说,我无法弄清楚为什么。

我在这里读到macOS 不区分大小写,而 ci 可能是,所以我仔细检查了所有的拼写,一切看起来都很好。

这是文件系统布局:

  src/
    App.jsx
    App.test.jsx
    components/
      A/
        index.js 
        B/
          C/
            CContainer.jsx 
            CContainer.test.jsx

下面是错误代码:

FAIL  src/components/A/B/C/CContainer.test.jsx


● Test suite failed to run

Cannot find module './CContainer' from 'CContainer.test.jsx'

  at Resolver.resolveModule (../../../tmp/node_modules/jest-resolve/build/index.js:179:17)
  at Object.<anonymous> (src/components/A/B/C/CContainer.test.jsx:3:29)

我的代码来自CContainer.test.jsx

import React from 'react';
import { shallow } from 'enzyme';
import { CContainer } from './CContainer';

const props = {
  saveTempUser: () => '',
  history: {},
  user: {},
};

it('renders without crashing', () => {
  shallow(<CContainer {...props} />);
});

这两个文件是兄弟姐妹,CContainer很简单:

...
export class CContainer extends Component {
...
}
...
export default connect(mapStateToProps, mapDispatchToProps)(CContainer)

奇怪的是在顶层App.test.js,同一个文件会导致问题:

 FAIL  src/App.test.js


● Test suite failed to run

Cannot find module './B/C/CContainer' from 'index.js'

  at Resolver.resolveModule (../../../tmp/node_modules/jest-resolve/build/index.js:179:17)
  at Object.<anonymous> (src/components/A/index.js:4:29)

而且index.js只是:

import ConnectedCContainer, { CContainer} from './B/C/CContainer'
...


export {
  ...
  ConnectedCContainer,
  CContainer
}.

有任何想法吗?我目前正在配置一个本地运行器来尝试调试,当我有一些信息时会回来提供更多信息。

标签: javascriptreactjsgitlabjestjsgitlab-ci

解决方案


事实证明,文件的情况与 git 中的不同。从 git 中删除文件并添加具有所需案例的新文件解决了问题。


推荐阅读