Open5

jest の test をよんでみたい

はじめに

Javascript の テスティングツール といえば、真っ先に jest が浮かびます。
ところが、 jest を完璧に使いこなせている!と思う方は少ないと思います。
そこで、 jest に記述されている test コードを読むことで、どのようなテストを書くべきなのかを読み取っていきたいです。


テストのテストなので、少し違うケースもあるかもしれませんが読んでみたいので、一旦まとめて、後で振り返ります。

jest について

jest は facebook が提供している Javascript のテスティングツールです。
リポジトリ facebook/jest

テストを含むディレクトリを一覧で出してみた。
一旦 examples について読んでみる。次に packages/xxx or e2e/xxx のどちらかを読み進める

__tests__ を含んでいるディレクトリ
examples/snapshot
examples/react-testing-library
examples/react-native
examples/typescript
examples/timer
examples/jquery
examples/async
examples/manual-mocks
examples/module-mock
examples/enzyme
examples/mongodb
examples/automatic-mocks
examples/react
packages/jest-runtime/src
packages/jest-matcher-utils/src
packages/jest-fake-timers/src
packages/jest-docblock/src
packages/diff-sequences/src
packages/jest-worker/src
packages/jest-worker/src/workers
packages/jest-worker/src/base
packages/jest-cli/src/init
packages/jest-cli/src
packages/jest-circus/src
packages/jest-reporters/src
packages/jest-globals/src
packages/babel-jest/src
packages/jest-source-map/src
packages/jest-resolve/src
packages/jest-watcher/src/lib
packages/jest-create-cache-key-function/src
packages/jest-transform/src
packages/jest-serializer/src
packages/jest-runner/src
packages/jest-leak-detector/src
packages/jest-haste-map/src/crawlers
packages/jest-haste-map/src/crawlers
packages/jest-haste-map/src
packages/jest-haste-map/src/lib
packages/jest-console/src
packages/jest-environment-node/src
packages/jest-util/src
packages/jest-get-type/src
packages/jest-get-type/src
packages/jest-core/src/plugins
packages/jest-core/src
packages/jest-core/src/lib
packages/jest-message-util/src
packages/jest-validate/src
packages/jest-test-sequencer/src
packages/pretty-format/src
packages/jest-each/src
packages/jest-mock/src
packages/babel-plugin-jest-hoist/src
packages/jest-jasmine2/src
packages/jest-diff/src
packages/jest-resolve-dependencies/src
packages/jest-config/src
packages/jest-regex-util/src
packages/jest-environment-jsdom/src
packages/jest-test-result/src
packages/jest-repl/src
packages/expect/src
e2e/circus-declaration-errors
e2e/error-on-deprecated
e2e/select-projects-missing-name
e2e/select-projects-missing-name
e2e/clear-cache
e2e/chai-assertion-library-errors
e2e/test-environment-esm
e2e/mock-json
e2e/failures
e2e/resolve-with-paths
e2e/compare-dom-nodes
e2e/custom-reporters
e2e/empty-describe-with-hooks
e2e/imported-globals
e2e/snapshot
e2e/snapshot
e2e/timer-use-real-timers
e2e/done-in-hooks
e2e/modern-fake-timers/from-jest-object
e2e/modern-fake-timers/from-config
e2e/mock-names/without-mock-name
e2e/mock-names/with-mock-name-call-times-fail
e2e/mock-names/with-mock-name-not-called
e2e/mock-names/with-mock-name-call-times-pass
e2e/mock-names/with-empty-mock-name-not-called
e2e/mock-names/with-mock-name-not-called-fail
e2e/mock-names/with-mock-name-not-called-pass
e2e/mock-names/with-empty-mock-name
e2e/mock-names/with-mock-name
e2e/mock-names/without-mock-name-not-called
e2e/transform-linked-modules
e2e/failureDetails-property
e2e/wrong-env
e2e/global-setup
e2e/custom-inline-snapshot-matchers
e2e/test-environment
e2e/test-environment-async
e2e/require-main-isolate-modules
e2e/select-projects
e2e/auto-reset-mocks/with-auto-reset
e2e/auto-reset-mocks/without-auto-reset
e2e/setup-files-after-env-config
e2e/setup-files-after-env-config
e2e/setup-files-after-env-config
e2e/module-name-mapper-wrong-config
e2e/resolve-get-paths
e2e/stack-trace-no-capture-stack-trace
e2e/resolve
e2e/expect-in-vm
e2e/typescript-coverage
e2e/node-path
e2e/coverage-transform-instrumented
e2e/test-name-pattern
e2e/custom-matcher-stack-trace
e2e/custom-matcher-stack-trace
e2e/override-globals
e2e/auto-clear-mocks/with-auto-clear
e2e/auto-clear-mocks/without-auto-clear
e2e/resolve-node-module
e2e/require-main-after-create-require
e2e/fake-time/legacy/requestAnimationFrame
e2e/fake-time/legacy/from-jest-object
e2e/fake-time/legacy/from-config
e2e/fake-time/modern/requestAnimationFrame
e2e/fake-time/modern/from-jest-object
e2e/fake-time/modern/from-config
e2e/native-async-mock
e2e/expect-async-matcher
e2e/expect-async-matcher
e2e/environmentOptions
e2e/runtime-internal-module-registry
e2e/each
e2e/auto-restore-mocks/with-auto-restore
e2e/auto-restore-mocks/without-auto-restore
e2e/stack-trace
e2e/global-setup-esm
e2e/coverage-provider-v8/esm-native-without-sourcemap
e2e/coverage-provider-v8/cjs-with-babel-transformer
e2e/coverage-provider-v8/esm-with-custom-transformer
e2e/coverage-provider-v8/no-sourcemap
e2e/coverage-provider-v8/empty-sourcemap
e2e/coverage-provider-v8/cjs-native-without-sourcemap
e2e/resolve-conditions
e2e/test-name-pattern-skipped
e2e/module-name-mapper-wrong-array-config
e2e/create-process-object
e2e/before-all-filtered
e2e/test-environment-run-script
e2e/test-todo
e2e/global-teardown-esm
e2e/test-environment-circus
e2e/snapshot-unknown
e2e/module-name-mapper-correct-config
e2e/set-immediate
e2e/stack-trace-source-maps-with-coverage
e2e/nested-event-loop
e2e/module-parent-null-in-test
e2e/fake-promises/asap
e2e/fake-promises/immediate
e2e/json-reporter
e2e/custom-jsdom-html
e2e/require-main
e2e
e2e/dom-diffing
e2e/require-main-reset-modules
e2e/list-tests
e2e/stack-trace-source-maps
e2e/call-done-twice
e2e/regex-(char-in-path
e2e/focused-tests
e2e/promise-and-callback
e2e/global-setup-node-modules
e2e/environment-after-teardown
e2e/custom-resolver
e2e/test-environment-circus-async
e2e/location-in-results
e2e/snapshot-escape
e2e/esm-config/js
e2e/esm-config/mjs
e2e/esm-config/cjs
e2e/coverage-handlebars
e2e/coverage-without-transform
e2e/snapshot-formatting-changes
e2e/snapshot-formatting-changes
e2e/lifecycles
e2e/console-jsdom
e2e/multi-project-config-root/foo
e2e/multi-project-config-root/bar
e2e/require-after-teardown
e2e/detect-open-handles
e2e/console-winston
e2e/global-teardown
e2e/async-regenerator
e2e/snapshot-resolver
e2e/global-setup-custom-transform
e2e/native-esm-typescript
e2e/console-after-teardown
e2e/before-all-skipped
e2e/watch-plugins/js-type-module
e2e/watch-plugins/js
e2e/watch-plugins/mjs
e2e/watch-plugins/cjs
e2e/resolve-no-extensions
e2e/snapshot-serializers
e2e/dynamic-require-dependencies
e2e/config-override
e2e/filter
e2e/custom-haste-map
e2e/env-test
e2e/babel-plugin-jest-hoist
e2e/browser-resolver
e2e/test-results-processor
e2e/snapshot-mock-fs
e2e/module-name-mapper-mock
e2e/module-name-mapper-mock
e2e/module-name-mapper-correct-mock-absolute-path
e2e/presets/js-type-module
e2e/presets/js
e2e/presets/json
e2e/presets/mjs
e2e/presets/cjs
e2e/empty-suite-error
e2e/generator-mock
e2e/jasmine-async
e2e/verbose-reporter
e2e/coverage-remapping
e2e/console-debugging
e2e/transform/esm-transformer
e2e/transform/transform-testrunner
e2e/transform/cache
e2e/transform/babel-jest-ignored
e2e/transform/ecmascript-modules-support
e2e/transform/transform-runner
e2e/transform/babel-jest
e2e/transform/babel-jest
e2e/transform/custom-instrumenting-preprocessor
e2e/transform/transform-environment
e2e/transform/babel-jest-async
e2e/transform/transformer-config
e2e/transform/multiple-transformers
e2e/transform/multiple-transformers
e2e/transform/transform-snapshotResolver
e2e/transform/babel-jest-manual
e2e/transform/transform-esm-testrunner
e2e/transform/no-babel-jest
e2e/transform/no-babel-jest
e2e/transform/async-transformer
e2e/transform/transform-esm-runner
e2e/multiple-configs
e2e/circus-concurrent
e2e/coverage-report/cached-duplicates/a
e2e/coverage-report/cached-duplicates/b
e2e/coverage-report
e2e/before-each-queue
e2e/pnp
e2e/reset-modules
e2e/nested-test-definitions
e2e/declaration-errors
e2e/process-exit
e2e/test-retries
e2e/bad-source-map
e2e/console
e2e/native-esm

examples

examples ディレクトリ以下の調査

about Vanilla js

getting-started


async

正常系

  • 同期処理で返ってくる値を確かめるテストgithub
  • 非同期処理で返ってくる値を確かめるテストgithub

異常系

  • 同期処理で返ってくる値を確かめるテストgithub
  • 非同期処理で返ってくる値を確かめるテストgithub
    • xxx().catch / try { xxx() } catch (e) { } / expect(xxx).rejects.toEqual()

timer

about React

react

react-testing-library

作成者以外のコメントは許可されていません