Best JavaScript code snippet using jest
ScriptTransformer.js
Source:ScriptTransformer.js
...251 writeCacheFile(sourceMapPath, sourceMapContent);252 } else {253 sourceMapPath = null;254 }255 writeCodeCacheFile(cacheFilePath, code);256 return {257 code,258 mapCoverage,259 sourceMapPath,260 };261 }262 _transformAndBuildScript(263 filename: Path,264 options: ?Options,265 instrument: boolean,266 fileSource?: string,267 ): TransformResult {268 const isInternalModule = !!(options && options.isInternalModule);269 const isCoreModule = !!(options && options.isCoreModule);270 const content = stripShebang(271 fileSource || fs.readFileSync(filename, 'utf8'),272 );273 let wrappedCode: string;274 let sourceMapPath: ?string = null;275 let mapCoverage = false;276 const willTransform =277 !isInternalModule &&278 !isCoreModule &&279 (this._shouldTransform(filename) || instrument);280 try {281 const extraGlobals = (options && options.extraGlobals) || [];282 if (willTransform) {283 const transformedSource = this.transformSource(284 filename,285 content,286 instrument,287 );288 wrappedCode = wrap(transformedSource.code, ...extraGlobals);289 sourceMapPath = transformedSource.sourceMapPath;290 mapCoverage = transformedSource.mapCoverage;291 } else {292 wrappedCode = wrap(content, ...extraGlobals);293 }294 return {295 mapCoverage,296 script: new vm.Script(wrappedCode, {297 displayErrors: true,298 filename: isCoreModule ? 'jest-nodejs-core-' + filename : filename,299 }),300 sourceMapPath,301 };302 } catch (e) {303 if (e.codeFrame) {304 e.stack = e.message + '\n' + e.codeFrame;305 }306 if (307 e instanceof SyntaxError &&308 e.message.includes('Unexpected token') &&309 !e.message.includes(' expected')310 ) {311 throw enhanceUnexpectedTokenMessage(e);312 }313 throw e;314 }315 }316 transform(317 filename: Path,318 options: Options,319 fileSource?: string,320 ): TransformResult {321 let scriptCacheKey = null;322 let instrument = false;323 let result = '';324 if (!options.isCoreModule) {325 instrument = shouldInstrument(filename, options, this._config);326 scriptCacheKey = getScriptCacheKey(filename, instrument);327 result = this._cache.transformedFiles.get(scriptCacheKey);328 }329 if (result) {330 return result;331 }332 result = this._transformAndBuildScript(333 filename,334 options,335 instrument,336 fileSource,337 );338 if (scriptCacheKey) {339 this._cache.transformedFiles.set(scriptCacheKey, result);340 }341 return result;342 }343 _shouldTransform(filename: Path): boolean {344 const ignoreRegexp = this._cache.ignorePatternsRegExp;345 const isIgnored = ignoreRegexp ? ignoreRegexp.test(filename) : false;346 return (347 !!this._config.transform && !!this._config.transform.length && !isIgnored348 );349 }350}351const removeFile = (path: Path) => {352 try {353 fs.unlinkSync(path);354 } catch (e) {}355};356const stripShebang = content => {357 // If the file data starts with a shebang remove it. Leaves the empty line358 // to keep stack trace line numbers correct.359 if (content.startsWith('#!')) {360 return content.replace(/^#!.*/, '');361 } else {362 return content;363 }364};365/**366 * This is like `writeCacheFile` but with an additional sanity checksum. We367 * cannot use the same technique for source maps because we expose source map368 * cache file paths directly to callsites, with the expectation they can read369 * it right away. This is not a great system, because source map cache file370 * could get corrupted, out-of-sync, etc.371 */372function writeCodeCacheFile(cachePath: Path, code: string) {373 const checksum = crypto374 .createHash('md5')375 .update(code)376 .digest('hex');377 writeCacheFile(cachePath, checksum + '\n' + code);378}379/**380 * Read counterpart of `writeCodeCacheFile`. We verify that the content of the381 * file matches the checksum, in case some kind of corruption happened. This382 * could happen if an older version of `jest-runtime` writes non-atomically to383 * the same cache, for example.384 */385function readCodeCacheFile(cachePath: Path): ?string {386 const content = readCacheFile(cachePath);...
script_transformer.js
Source:script_transformer.js
...235 writeCacheFile(sourceMapPath, sourceMapContent);236 } else {237 sourceMapPath = null;238 }239 writeCodeCacheFile(cacheFilePath, code);240 return {241 code,242 sourceMapPath,243 };244 }245 _transformAndBuildScript(246 filename: Path,247 options: ?Options,248 instrument: boolean,249 fileSource?: string,250 ): TransformResult {251 const isInternalModule = !!(options && options.isInternalModule);252 const content = stripShebang(253 fileSource || fs.readFileSync(filename, 'utf8'),254 );255 let wrappedCode: string;256 let sourceMapPath: ?string = null;257 const willTransform =258 !isInternalModule &&259 (shouldTransform(filename, this._config) || instrument);260 try {261 if (willTransform) {262 const transformedSource = this.transformSource(263 filename,264 content,265 instrument,266 !!(options && options.mapCoverage),267 );268 wrappedCode = wrap(transformedSource.code);269 sourceMapPath = transformedSource.sourceMapPath;270 } else {271 wrappedCode = wrap(content);272 }273 return {274 script: new vm.Script(wrappedCode, {displayErrors: true, filename}),275 sourceMapPath,276 };277 } catch (e) {278 if (e.codeFrame) {279 e.stack = e.codeFrame;280 }281 throw e;282 }283 }284 transform(285 filename: Path,286 options: Options,287 fileSource?: string,288 ): TransformResult {289 const instrument = shouldInstrument(filename, options, this._config);290 const scriptCacheKey = getScriptCacheKey(291 filename,292 this._config,293 instrument,294 );295 let result = cache.get(scriptCacheKey);296 if (result) {297 return result;298 } else {299 result = this._transformAndBuildScript(300 filename,301 options,302 instrument,303 fileSource,304 );305 cache.set(scriptCacheKey, result);306 return result;307 }308 }309}310const removeFile = (path: Path) => {311 try {312 fs.unlinkSync(path);313 } catch (e) {}314};315const stripShebang = content => {316 // If the file data starts with a shebang remove it. Leaves the empty line317 // to keep stack trace line numbers correct.318 if (content.startsWith('#!')) {319 return content.replace(/^#!.*/, '');320 } else {321 return content;322 }323};324/**325 * This is like `writeCacheFile` but with an additional sanity checksum. We326 * cannot use the same technique for source maps because we expose source map327 * cache file paths directly to callsites, with the expectation they can read328 * it right away. This is not a great system, because source map cache file329 * could get corrupted, out-of-sync, etc.330 */331function writeCodeCacheFile(cachePath: Path, code: string) {332 const checksum = crypto.createHash('md5').update(code).digest('hex');333 writeCacheFile(cachePath, checksum + '\n' + code);334}335/**336 * Read counterpart of `writeCodeCacheFile`. We verify that the content of the337 * file matches the checksum, in case some kind of corruption happened. This338 * could happen if an older version of `jest-runtime` writes non-atomically to339 * the same cache, for example.340 */341function readCodeCacheFile(cachePath: Path): ?string {342 const content = readCacheFile(cachePath);343 if (content == null) {344 return null;345 }...
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!!