Best JavaScript code snippet using jest
node_watcher.js
Source:node_watcher.js
...118 * @param error119 * @private120 */121 checkedEmitError(error) {122 if (!isIgnorableFileError(error)) {123 this.emit('error', error);124 }125 }126 /**127 * Watch a directory.128 *129 * @param {string} dir130 * @private131 */132 watchdir(dir) {133 if (this.watched[dir]) {134 return;135 }136 let watcher = fs.watch(137 dir,138 { persistent: true },139 this.normalizeChange.bind(this, dir)140 );141 this.watched[dir] = watcher;142 watcher.on('error', this.checkedEmitError);143 if (this.root !== dir) {144 this.register(dir);145 }146 }147 /**148 * Stop watching a directory.149 *150 * @param {string} dir151 * @private152 */153 stopWatching(dir) {154 if (this.watched[dir]) {155 this.watched[dir].close();156 delete this.watched[dir];157 }158 }159 /**160 * End watching.161 *162 * @public163 */164 close(callback) {165 Object.keys(this.watched).forEach(this.stopWatching, this);166 this.removeAllListeners();167 if (typeof callback === 'function') {168 setImmediate(callback.bind(null, null, true));169 }170 }171 /**172 * On some platforms, as pointed out on the fs docs (most likely just win32)173 * the file argument might be missing from the fs event. Try to detect what174 * change by detecting if something was deleted or the most recent file change.175 *176 * @param {string} dir177 * @param {string} event178 * @param {string} file179 * @public180 */181 detectChangedFile(dir, event, callback) {182 if (!this.dirRegistery[dir]) {183 return;184 }185 let found = false;186 let closest = { mtime: 0 };187 let c = 0;188 Object.keys(this.dirRegistery[dir]).forEach(function(file, i, arr) {189 fs.lstat(190 path.join(dir, file),191 function(error, stat) {192 if (found) {193 return;194 }195 if (error) {196 if (isIgnorableFileError(error)) {197 found = true;198 callback(file);199 } else {200 this.emit('error', error);201 }202 } else {203 if (stat.mtime > closest.mtime) {204 stat.file = file;205 closest = stat;206 }207 if (arr.length === ++c) {208 callback(closest.file);209 }210 }211 }.bind(this)212 );213 }, this);214 }215 /**216 * Normalize fs events and pass it on to be processed.217 *218 * @param {string} dir219 * @param {string} event220 * @param {string} file221 * @public222 */223 normalizeChange(dir, event, file) {224 if (!file) {225 this.detectChangedFile(226 dir,227 event,228 function(actualFile) {229 if (actualFile) {230 this.processChange(dir, event, actualFile);231 }232 }.bind(this)233 );234 } else {235 this.processChange(dir, event, path.normalize(file));236 }237 }238 /**239 * Process changes.240 *241 * @param {string} dir242 * @param {string} event243 * @param {string} file244 * @public245 */246 processChange(dir, event, file) {247 let fullPath = path.join(dir, file);248 let relativePath = path.join(path.relative(this.root, dir), file);249 fs.lstat(250 fullPath,251 function(error, stat) {252 if (error && error.code !== 'ENOENT') {253 this.emit('error', error);254 } else if (!error && stat.isDirectory()) {255 // win32 emits usless change events on dirs.256 if (event !== 'change') {257 this.watchdir(fullPath);258 if (259 common.isFileIncluded(260 this.globs,261 this.dot,262 this.doIgnore,263 relativePath264 )265 ) {266 this.emitEvent(ADD_EVENT, relativePath, stat);267 }268 }269 } else {270 let registered = this.registered(fullPath);271 if (error && error.code === 'ENOENT') {272 this.unregister(fullPath);273 this.stopWatching(fullPath);274 this.unregisterDir(fullPath);275 if (registered) {276 this.emitEvent(DELETE_EVENT, relativePath);277 }278 } else if (registered) {279 this.emitEvent(CHANGE_EVENT, relativePath, stat);280 } else {281 if (this.register(fullPath)) {282 this.emitEvent(ADD_EVENT, relativePath, stat);283 }284 }285 }286 }.bind(this)287 );288 }289 /**290 * Triggers a 'change' event after debounding it to take care of duplicate291 * events on os x.292 *293 * @private294 */295 emitEvent(type, file, stat) {296 let key = type + '-' + file;297 let addKey = ADD_EVENT + '-' + file;298 if (type === CHANGE_EVENT && this.changeTimers[addKey]) {299 // Ignore the change event that is immediately fired after an add event.300 // (This happens on Linux).301 return;302 }303 clearTimeout(this.changeTimers[key]);304 this.changeTimers[key] = setTimeout(305 function() {306 delete this.changeTimers[key];307 if (type === ADD_EVENT && stat.isDirectory()) {308 // Recursively emit add events and watch for sub-files/folders309 common.recReaddir(310 path.resolve(this.root, file),311 function emitAddDir(dir, stats) {312 this.watchdir(dir);313 this.rawEmitEvent(314 ADD_EVENT,315 path.relative(this.root, dir),316 stats317 );318 }.bind(this),319 function emitAddFile(file, stats) {320 this.register(file);321 this.rawEmitEvent(322 ADD_EVENT,323 path.relative(this.root, file),324 stats325 );326 }.bind(this),327 function endCallback() {},328 this.checkedEmitError,329 this.ignored330 );331 } else {332 this.rawEmitEvent(type, file, stat);333 }334 }.bind(this),335 DEFAULT_DELAY336 );337 }338 /**339 * Actually emit the events340 */341 rawEmitEvent(type, file, stat) {342 this.emit(type, file, this.root, stat);343 this.emit(ALL_EVENT, type, file, this.root, stat);344 }345};346/**347 * Determine if a given FS error can be ignored348 *349 * @private350 */351function isIgnorableFileError(error) {352 return (353 error.code === 'ENOENT' ||354 // Workaround Windows node issue #4337.355 (error.code === 'EPERM' && platform === 'win32')356 );...
NodeWatcher.js
Source:NodeWatcher.js
...119 * @param error120 * @private121 */122 checkedEmitError(error) {123 if (!isIgnorableFileError(error)) {124 this.emit('error', error);125 }126 }127 /**128 * Watch a directory.129 *130 * @param {string} dir131 * @private132 */133 watchdir(dir) {134 if (this.watched[dir]) {135 return;136 }137 const watcher = fs.watch(138 dir,139 {140 persistent: true141 },142 this.normalizeChange.bind(this, dir)143 );144 this.watched[dir] = watcher;145 watcher.on('error', this.checkedEmitError);146 if (this.root !== dir) {147 this.register(dir);148 }149 }150 /**151 * Stop watching a directory.152 *153 * @param {string} dir154 * @private155 */156 stopWatching(dir) {157 if (this.watched[dir]) {158 this.watched[dir].close();159 delete this.watched[dir];160 }161 }162 /**163 * End watching.164 *165 * @public166 */167 close() {168 Object.keys(this.watched).forEach(this.stopWatching, this);169 this.removeAllListeners();170 return Promise.resolve();171 }172 /**173 * On some platforms, as pointed out on the fs docs (most likely just win32)174 * the file argument might be missing from the fs event. Try to detect what175 * change by detecting if something was deleted or the most recent file change.176 *177 * @param {string} dir178 * @param {string} event179 * @param {string} file180 * @public181 */182 detectChangedFile(dir, event, callback) {183 if (!this.dirRegistery[dir]) {184 return;185 }186 let found = false;187 let closest = {188 mtime: 0189 };190 let c = 0;191 Object.keys(this.dirRegistery[dir]).forEach(function (file, i, arr) {192 fs.lstat(path.join(dir, file), (error, stat) => {193 if (found) {194 return;195 }196 if (error) {197 if (isIgnorableFileError(error)) {198 found = true;199 callback(file);200 } else {201 this.emit('error', error);202 }203 } else {204 if (stat.mtime > closest.mtime) {205 stat.file = file;206 closest = stat;207 }208 if (arr.length === ++c) {209 callback(closest.file);210 }211 }212 });213 }, this);214 }215 /**216 * Normalize fs events and pass it on to be processed.217 *218 * @param {string} dir219 * @param {string} event220 * @param {string} file221 * @public222 */223 normalizeChange(dir, event, file) {224 if (!file) {225 this.detectChangedFile(dir, event, actualFile => {226 if (actualFile) {227 this.processChange(dir, event, actualFile);228 }229 });230 } else {231 this.processChange(dir, event, path.normalize(file));232 }233 }234 /**235 * Process changes.236 *237 * @param {string} dir238 * @param {string} event239 * @param {string} file240 * @public241 */242 processChange(dir, event, file) {243 const fullPath = path.join(dir, file);244 const relativePath = path.join(path.relative(this.root, dir), file);245 fs.lstat(fullPath, (error, stat) => {246 if (error && error.code !== 'ENOENT') {247 this.emit('error', error);248 } else if (!error && stat.isDirectory()) {249 // win32 emits usless change events on dirs.250 if (event !== 'change') {251 this.watchdir(fullPath);252 if (253 common.isFileIncluded(254 this.globs,255 this.dot,256 this.doIgnore,257 relativePath258 )259 ) {260 this.emitEvent(ADD_EVENT, relativePath, stat);261 }262 }263 } else {264 const registered = this.registered(fullPath);265 if (error && error.code === 'ENOENT') {266 this.unregister(fullPath);267 this.stopWatching(fullPath);268 this.unregisterDir(fullPath);269 if (registered) {270 this.emitEvent(DELETE_EVENT, relativePath);271 }272 } else if (registered) {273 this.emitEvent(CHANGE_EVENT, relativePath, stat);274 } else {275 if (this.register(fullPath)) {276 this.emitEvent(ADD_EVENT, relativePath, stat);277 }278 }279 }280 });281 }282 /**283 * Triggers a 'change' event after debounding it to take care of duplicate284 * events on os x.285 *286 * @private287 */288 emitEvent(type, file, stat) {289 const key = type + '-' + file;290 const addKey = ADD_EVENT + '-' + file;291 if (type === CHANGE_EVENT && this.changeTimers[addKey]) {292 // Ignore the change event that is immediately fired after an add event.293 // (This happens on Linux).294 return;295 }296 clearTimeout(this.changeTimers[key]);297 this.changeTimers[key] = setTimeout(() => {298 delete this.changeTimers[key];299 if (type === ADD_EVENT && stat.isDirectory()) {300 // Recursively emit add events and watch for sub-files/folders301 common.recReaddir(302 path.resolve(this.root, file),303 function emitAddDir(dir, stats) {304 this.watchdir(dir);305 this.rawEmitEvent(ADD_EVENT, path.relative(this.root, dir), stats);306 }.bind(this),307 function emitAddFile(file, stats) {308 this.register(file);309 this.rawEmitEvent(ADD_EVENT, path.relative(this.root, file), stats);310 }.bind(this),311 function endCallback() {},312 this.checkedEmitError,313 this.ignored314 );315 } else {316 this.rawEmitEvent(type, file, stat);317 }318 }, DEFAULT_DELAY);319 }320 /**321 * Actually emit the events322 */323 rawEmitEvent(type, file, stat) {324 this.emit(type, file, this.root, stat);325 this.emit(ALL_EVENT, type, file, this.root, stat);326 }327};328/**329 * Determine if a given FS error can be ignored330 *331 * @private332 */333function isIgnorableFileError(error) {334 return (335 error.code === 'ENOENT' || // Workaround Windows node issue #4337.336 (error.code === 'EPERM' && platform === 'win32')337 );...
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!!