CI 正在运行样本纱线测试

Posted

技术标签:

【中文标题】CI 正在运行样本纱线测试【英文标题】:CI is running sample yarn tests 【发布时间】:2021-04-21 01:05:31 【问题描述】:

我对 Jest 比较陌生,并试图在我的 GitLab CI 上为 Rails 应用程序运行它。当我在本地运行测试时,它们运行得很好。我使用这个命令来运行它们:

yarn run test

AFAIK,这和跑步一样:

npm run test

当我在 CI 上运行时,测试通过了,但它也在 Gems 等内部运行各种示例测试。请参阅下面的日志。

我为什么要运行示例测试以及如何停止它?

$ yarn run test
yarn run v1.22.4
$ jest
FAIL vendor/ruby/2.7.0/gems/capybara-3.14.0/lib/capybara/spec/public/test.js
  ● Test suite failed to run
    ReferenceError: $ is not defined
      1 | var activeRequests = 0;
    > 2 | $(function() 
        | ^
      3 |   $('#change').text('I changed it');
      4 |   $('#drag, #drag_scroll, #drag_link').draggable();
      5 |   $('#drop, #drop_scroll').droppable(
      at Object.<anonymous> (vendor/ruby/2.7.0/gems/capybara-3.14.0/lib/capybara/spec/public/test.js:2:1)
PASS app/javascript/reactjs/article/AudioContent.test.js
FAIL vendor/ruby/2.7.0/gems/webpacker-5.2.1/package/environments/__tests__/base.js
  ● Environment › toWebpackConfig › should return multi file entry points
    TypeError: Cannot read property 'sort' of undefined
      32 |     test('should return multi file entry points', () => 
      33 |       const config = environment.toWebpackConfig()
    > 34 |       expect(config.entry.multi_entry.sort()).toEqual([
         |                                       ^
      35 |         resolve('app', 'javascript', 'packs', 'multi_entry.css'),
      36 |         resolve('app', 'javascript', 'packs', 'multi_entry.js')
      37 |       ])
      at Object.<anonymous> (vendor/ruby/2.7.0/gems/webpacker-5.2.1/package/environments/__tests__/base.js:34:39)
  ● Environment › toWebpackConfig › should return default resolve.modules with additions
    expect(received).toEqual(expected) // deep equality
    - Expected  - 3
    + Received  + 0
      Array [
        "/builds/immersive-apps/immersive/app/javascript",
    -   "/builds/immersive-apps/immersive/app/assets",
    -   "/etc/yarn",
    -   "/builds/immersive-apps/immersive/app/elm",
        "node_modules",
      ]
      83 |     test('should return default resolve.modules with additions', () => 
      84 |       const config = environment.toWebpackConfig()
    > 85 |       expect(config.resolve.modules).toEqual([
         |                                      ^
      86 |         resolve('app', 'javascript'),
      87 |         resolve('app/assets'),
      88 |         resolve('/etc/yarn'),
      at Object.<anonymous> (vendor/ruby/2.7.0/gems/webpacker-5.2.1/package/environments/__tests__/base.js:85:38)
PASS vendor/ruby/2.7.0/gems/webpacker-5.2.1/package/config_types/__tests__/config_list.js
PASS app/javascript/reactjs/article/ArticleHeader.test.js
PASS vendor/ruby/2.7.0/gems/webpacker-5.2.1/package/utils/__tests__/get_style_rule.js
PASS app/javascript/reactjs/article/AudioPlayer.test.js
FAIL vendor/ruby/2.7.0/gems/webpacker-5.2.1/package/__tests__/config.js
  ● Config › should return additional paths as listed in app config, with resolved paths
    expect(received).toEqual(expected) // deep equality
    - Expected  - 5
    + Received  + 1
    - Array [
    -   "app/assets",
    -   "/etc/yarn",
    -   "app/elm",
    - ]
    + Array []
      25 | 
      26 |   test('should return additional paths as listed in app config, with resolved paths', () => 
    > 27 |     expect(config.additional_paths).toEqual(
         |                                     ^
      28 |       [
      29 |         'app/assets',
      30 |         '/etc/yarn',
      at Object.<anonymous> (vendor/ruby/2.7.0/gems/webpacker-5.2.1/package/__tests__/config.js:27:37)
  ● Config › should return extensions as listed in app config
    expect(received).toEqual(expected) // deep equality
    - Expected  - 1
    + Received  + 1
    @@ -1,6 +1,7 @@
      Array [
    +   ".jsx",
        ".mjs",
        ".js",
        ".sass",
        ".scss",
        ".css",
    @@ -10,7 +11,6 @@
        ".png",
        ".svg",
        ".gif",
        ".jpeg",
        ".jpg",
    -   ".elm",
      ]
      35 | 
      36 |   test('should return extensions as listed in app config', () => 
    > 37 |     expect(config.extensions).toEqual([
         |                               ^
      38 |       '.mjs',
      39 |       '.js',
      40 |       '.sass',
      at Object.<anonymous> (vendor/ruby/2.7.0/gems/webpacker-5.2.1/package/__tests__/config.js:37:31)
  ● Config › should return static assets extensions as listed in app config
    expect(received).toEqual(expected) // deep equality
    - Expected  - 0
    + Received  + 5
    @@ -4,6 +4,11 @@
        ".png",
        ".gif",
        ".tiff",
        ".ico",
        ".svg",
    +   ".eot",
    +   ".otf",
    +   ".ttf",
    +   ".woff",
    +   ".woff2",
      ]
      54 | 
      55 |   test('should return static assets extensions as listed in app config', () => 
    > 56 |     expect(config.static_assets_extensions).toEqual([
         |                                             ^
      57 |       '.jpg',
      58 |       '.jpeg',
      59 |       '.png',
      at Object.<anonymous> (vendor/ruby/2.7.0/gems/webpacker-5.2.1/package/__tests__/config.js:56:45)
PASS vendor/ruby/2.7.0/gems/webpacker-5.2.1/package/__tests__/dev_server.js
PASS vendor/ruby/2.7.0/gems/webpacker-5.2.1/package/__tests__/development.js
FAIL vendor/ruby/2.7.0/gems/webpacker-5.2.1/package/__tests__/env.js
  ● Env › with a non-standard environment
    expect(received).toEqual(expected) // deep equality
    - Expected  - 1
    + Received  + 1
      Object 
        "nodeEnv": "production",
    -   "railsEnv": "staging",
    +   "railsEnv": "production",
      
      39 |     process.env.RAILS_ENV = 'staging'
      40 |     process.env.NODE_ENV = 'staging'
    > 41 |     expect(require('../env')).toEqual(
         |                               ^
      42 |       railsEnv: 'staging',
      43 |       nodeEnv: 'production'
      44 |     )
      at Object.<anonymous> (vendor/ruby/2.7.0/gems/webpacker-5.2.1/package/__tests__/env.js:41:31)
PASS vendor/ruby/2.7.0/gems/webpacker-5.2.1/package/config_types/__tests__/config_object.js
PASS vendor/ruby/2.7.0/gems/webpacker-5.2.1/package/utils/__tests__/deep_assign.js
FAIL vendor/ruby/2.7.0/gems/webpacker-5.2.1/package/__tests__/staging.js
  ● Custom environment › toWebpackConfig › should use staging config and default production environment
    expect(received).toEqual(expected) // deep equality
    Expected: "/builds/immersive-apps/immersive/public/packs-staging"
    Received: "/builds/immersive-apps/immersive/public/packs"
      19 |       const config = environment.toWebpackConfig()
      20 | 
    > 21 |       expect(config.output.path).toEqual(resolve('public', 'packs-staging'))
         |                                  ^
      22 |       expect(config.output.publicPath).toEqual('/packs-staging/')
      23 |       expect(config).toMatchObject(
      24 |         devtool: 'source-map',
      at Object.<anonymous> (vendor/ruby/2.7.0/gems/webpacker-5.2.1/package/__tests__/staging.js:21:34)
PASS vendor/ruby/2.7.0/gems/webpacker-5.2.1/package/__tests__/production.js
PASS vendor/ruby/2.7.0/gems/webpacker-5.2.1/package/__tests__/test.js
PASS vendor/ruby/2.7.0/gems/webpacker-5.2.1/package/utils/__tests__/deep_merge.js
PASS vendor/ruby/2.7.0/gems/webpacker-5.2.1/package/utils/__tests__/objectify.js
FAIL vendor/ruby/2.7.0/gems/webpacker-5.2.1/lib/install/config/webpack/test.js
  ● Test suite failed to run
    Your test suite must contain at least one test.
      at onResult (node_modules/@jest/core/build/TestScheduler.js:175:18)
      at node_modules/@jest/core/build/TestScheduler.js:304:17
      at node_modules/emittery/index.js:260:13
          at Array.map (<anonymous>)
      at Emittery.Typed.emit (node_modules/emittery/index.js:258:23)
FAIL vendor/ruby/2.7.0/gems/webpacker-5.2.1/package/environments/test.js
  ● Test suite failed to run
    Your test suite must contain at least one test.
      at onResult (node_modules/@jest/core/build/TestScheduler.js:175:18)
      at node_modules/@jest/core/build/TestScheduler.js:304:17
      at node_modules/emittery/index.js:260:13
          at Array.map (<anonymous>)
      at Emittery.Typed.emit (node_modules/emittery/index.js:258:23)
Test Suites: 7 failed, 13 passed, 20 total
Tests:       7 failed, 61 passed, 68 total

【问题讨论】:

我不知道该怎么做,但我想你应该对测试进行一些配置,告诉它忽略在vendor 目录中找到的任何测试。 jestjs.io/docs/en/… 顺便说一句,在包管理器之间移动通常不是一个好主意。当包解析发生并写入锁定文件时,很容易出现这样的情况:您使用一个包管理器成功构建它,而使用另一个包管理器则失败,特别是如果您将锁定文件提交给版本控制。如果你的 CI 环境使用了 yarn,那么你在本地开发时应该坚持使用 yarn。 感谢@ShafiqJetha 的评论,我正在全面使用纱线。奇怪的是我在本地没有这些问题,但在 CI 上却有。 【参考方案1】:

将此添加到 package.json

  "scripts": 
    "test": "jest --verbose ./app/javascript"
  ,

CI 正在运行。

【讨论】:

【参考方案2】:

你应该可以在你的 package.json 文件的 jest 部分添加这样的东西:

"jest": 
...
"testPathIgnorePatterns": [
  "<rootDir>/vendor/"
]

【讨论】:

以上是关于CI 正在运行样本纱线测试的主要内容,如果未能解决你的问题,请参考以下文章

PHP CI样本模型/控制器结构

为啥我的 .gitlab-ci.yml 在纱线测试中不断失败,并出现错误“/bin/sh: 1: react-scripts: not found”?

我如何确定JMeter一次不使用同一样本多次?

如何在具有纱线工作空间的 monorepo 中从 nodejs 项目构建 docker 映像

gitlab CI - 安装正确版本的纱线

贝叶斯分类器