Best JavaScript code snippet using jest
fileWalkers.js
Source:fileWalkers.js
...86 IPathType[(IPathType['DIRECTORY'] = 2)] = 'DIRECTORY';87 IPathType[(IPathType['OTHER'] = 3)] = 'OTHER';88})(IPathType || (IPathType = {}));89const checkedPaths = new Map();90function statSyncCached(path) {91 const result = checkedPaths.get(path);92 if (result != null) {93 return result;94 }95 let stat;96 try {97 // @ts-expect-error TS2554 - throwIfNoEntry is only available in recent version of node, but inclusion of the option is a backward compatible no-op.98 stat = fs().statSync(path, {99 throwIfNoEntry: false100 });101 } catch (e) {102 if (!(e && (e.code === 'ENOENT' || e.code === 'ENOTDIR'))) {103 throw e;104 }105 }106 if (stat) {107 if (stat.isFile() || stat.isFIFO()) {108 checkedPaths.set(path, IPathType.FILE);109 return IPathType.FILE;110 } else if (stat.isDirectory()) {111 checkedPaths.set(path, IPathType.DIRECTORY);112 return IPathType.DIRECTORY;113 }114 }115 checkedPaths.set(path, IPathType.OTHER);116 return IPathType.OTHER;117}118const checkedRealpathPaths = new Map();119function realpathCached(path) {120 let result = checkedRealpathPaths.get(path);121 if (result != null) {122 return result;123 }124 result = (0, _jestUtil().tryRealpath)(path);125 checkedRealpathPaths.set(path, result);126 if (path !== result) {127 // also cache the result in case it's ever referenced directly - no reason to `realpath` that as well128 checkedRealpathPaths.set(result, result);129 }130 return result;131}132const packageContents = new Map();133function readPackageCached(path) {134 let result = packageContents.get(path);135 if (result != null) {136 return result;137 }138 result = JSON.parse(fs().readFileSync(path, 'utf8'));139 packageContents.set(path, result);140 return result;141} // adapted from142// https://github.com/lukeed/escalade/blob/2477005062cdbd8407afc90d3f48f4930354252b/src/sync.js143// to use cached `fs` calls144function findClosestPackageJson(start) {145 let dir = (0, _path().resolve)('.', start);146 if (!isDirectory(dir)) {147 dir = (0, _path().dirname)(dir);148 }149 while (true) {150 const pkgJsonFile = (0, _path().resolve)(dir, './package.json');151 const hasPackageJson = isFile(pkgJsonFile);152 if (hasPackageJson) {153 return pkgJsonFile;154 }155 const prevDir = dir;156 dir = (0, _path().dirname)(dir);157 if (prevDir === dir) {158 return undefined;159 }160 }161}162/*163 * helper functions164 */165function isFile(file) {166 return statSyncCached(file) === IPathType.FILE;167}168function isDirectory(dir) {169 return statSyncCached(dir) === IPathType.DIRECTORY;170}171function realpathSync(file) {172 return realpathCached(file);...
defaultResolver.js
Source:defaultResolver.js
...109 IPathType[(IPathType['DIRECTORY'] = 2)] = 'DIRECTORY';110 IPathType[(IPathType['OTHER'] = 3)] = 'OTHER';111})(IPathType || (IPathType = {}));112const checkedPaths = new Map();113function statSyncCached(path) {114 const result = checkedPaths.get(path);115 if (result !== undefined) {116 return result;117 }118 let stat;119 try {120 stat = fs().statSync(path);121 } catch (e) {122 if (!(e && (e.code === 'ENOENT' || e.code === 'ENOTDIR'))) {123 throw e;124 }125 }126 if (stat) {127 if (stat.isFile() || stat.isFIFO()) {128 checkedPaths.set(path, IPathType.FILE);129 return IPathType.FILE;130 } else if (stat.isDirectory()) {131 checkedPaths.set(path, IPathType.DIRECTORY);132 return IPathType.DIRECTORY;133 }134 }135 checkedPaths.set(path, IPathType.OTHER);136 return IPathType.OTHER;137}138const checkedRealpathPaths = new Map();139function realpathCached(path) {140 let result = checkedRealpathPaths.get(path);141 if (result !== undefined) {142 return result;143 }144 result = (0, _jestUtil().tryRealpath)(path);145 checkedRealpathPaths.set(path, result);146 if (path !== result) {147 // also cache the result in case it's ever referenced directly - no reason to `realpath` that as well148 checkedRealpathPaths.set(result, result);149 }150 return result;151}152/*153 * helper functions154 */155function isFile(file) {156 return statSyncCached(file) === IPathType.FILE;157}158function isDirectory(dir) {159 return statSyncCached(dir) === IPathType.DIRECTORY;160}161function realpathSync(file) {162 return realpathCached(file);...
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!!