Best JavaScript code snippet using jest
utils.js
Source:utils.js
...144});145const jestEachBuildDir = (0, _slash.default)(146 path.dirname(require.resolve('jest-each'))147);148function takesDoneCallback(fn) {149 return fn.length > 0;150}151function isGeneratorFunction(fn) {152 return (0, _isGeneratorFn.default)(fn);153}154const makeDescribe = (name, parent, mode) => {155 let _mode = mode;156 if (parent && !mode) {157 // If not set explicitly, inherit from the parent describe.158 _mode = parent.mode;159 }160 return {161 type: 'describeBlock',162 // eslint-disable-next-line sort-keys163 children: [],164 hooks: [],165 mode: _mode,166 name: (0, _jestUtil.convertDescriptorToString)(name),167 parent,168 tests: []169 };170};171exports.makeDescribe = makeDescribe;172const makeTest = (fn, mode, name, parent, timeout, asyncError) => ({173 type: 'test',174 // eslint-disable-next-line sort-keys175 asyncError,176 duration: null,177 errors: [],178 fn,179 invocations: 0,180 mode,181 name: (0, _jestUtil.convertDescriptorToString)(name),182 parent,183 seenDone: false,184 startedAt: null,185 status: null,186 timeout187}); // Traverse the tree of describe blocks and return true if at least one describe188// block has an enabled test.189exports.makeTest = makeTest;190const hasEnabledTest = describeBlock => {191 const {hasFocusedTests, testNamePattern} = (0, _state.getState)();192 return describeBlock.children.some(child =>193 child.type === 'describeBlock'194 ? hasEnabledTest(child)195 : !(196 child.mode === 'skip' ||197 (hasFocusedTests && child.mode !== 'only') ||198 (testNamePattern && !testNamePattern.test(getTestID(child)))199 )200 );201};202const getAllHooksForDescribe = describe => {203 const result = {204 afterAll: [],205 beforeAll: []206 };207 if (hasEnabledTest(describe)) {208 for (const hook of describe.hooks) {209 switch (hook.type) {210 case 'beforeAll':211 result.beforeAll.push(hook);212 break;213 case 'afterAll':214 result.afterAll.push(hook);215 break;216 }217 }218 }219 return result;220};221exports.getAllHooksForDescribe = getAllHooksForDescribe;222const getEachHooksForTest = test => {223 const result = {224 afterEach: [],225 beforeEach: []226 };227 let block = test.parent;228 do {229 const beforeEachForCurrentBlock = []; // TODO: inline after https://github.com/microsoft/TypeScript/pull/34840 is released230 let hook;231 for (hook of block.hooks) {232 switch (hook.type) {233 case 'beforeEach':234 beforeEachForCurrentBlock.push(hook);235 break;236 case 'afterEach':237 result.afterEach.push(hook);238 break;239 }240 } // 'beforeEach' hooks are executed from top to bottom, the opposite of the241 // way we traversed it.242 result.beforeEach = [...beforeEachForCurrentBlock, ...result.beforeEach];243 } while ((block = block.parent));244 return result;245};246exports.getEachHooksForTest = getEachHooksForTest;247const describeBlockHasTests = describe =>248 describe.children.some(249 child => child.type === 'test' || describeBlockHasTests(child)250 );251exports.describeBlockHasTests = describeBlockHasTests;252const _makeTimeoutMessage = (timeout, isHook) =>253 `Exceeded timeout of ${(0, _jestUtil.formatTime)(timeout)} for a ${254 isHook ? 'hook' : 'test'255 }.\nUse jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test.`; // Global values can be overwritten by mocks or tests. We'll capture256// the original values in the variables before we require any files.257const {setTimeout, clearTimeout} = global;258function checkIsError(error) {259 return !!(error && error.message && error.stack);260}261const callAsyncCircusFn = (testOrHook, testContext, {isHook, timeout}) => {262 let timeoutID;263 let completed = false;264 const {fn, asyncError} = testOrHook;265 return new Promise((resolve, reject) => {266 timeoutID = setTimeout(267 () => reject(_makeTimeoutMessage(timeout, isHook)),268 timeout269 ); // If this fn accepts `done` callback we return a promise that fulfills as270 // soon as `done` called.271 if (takesDoneCallback(fn)) {272 let returnedValue = undefined;273 const done = reason => {274 // We need to keep a stack here before the promise tick275 const errorAtDone = new _jestUtil.ErrorWithStack(undefined, done);276 if (!completed && testOrHook.seenDone) {277 errorAtDone.message =278 'Expected done to be called once, but it was called multiple times.';279 if (reason) {280 errorAtDone.message +=281 ' Reason: ' +282 (0, _prettyFormat.format)(reason, {283 maxDepth: 3284 });285 }...
LambdaTest’s Jest Testing Tutorial covers step-by-step guides around Jest with code examples to help you be proficient with the Jest framework. The Jest tutorial has chapters to help you learn right from the basics of Jest framework to code-based tutorials around testing react apps with Jest, perform snapshot testing, import ES modules and more.
|<p>it('check_object_of_Car', () => {</p><p>
expect(newCar()).toBeInstanceOf(Car);</p><p>
});</p>|
| :- |
Get 100 minutes of automation test minutes FREE!!