Best JavaScript code snippet using sinon
index.js
Source:index.js
...63 fuseops64 .readdir(path)65 .then(response => {66 const { contents, contexts } = response;67 return process.nextTick(cb, 0, contents /*contexts*/);68 })69 .catch(err => {70 if (err instanceof FSError) {71 return process.nextTick(cb, err.errno);72 } else {73 return process.nextTick(cb, Fuse.EFAULT);74 }75 // //console.log(err)76 // return process.nextTick(cb, Fuse.ENOENT)77 });78 },79 getattr: (path, cb) => {80 //console.log('I>getattr', path);81 fuseops82 .getattr(path)83 .then(response => {84 // //console.log(response)85 return process.nextTick(cb, 0, response);86 })87 .catch(err => {88 //console.log('I>getattr error',path);89 if (err instanceof FSError) {90 return process.nextTick(cb, err.errno);91 } else {92 return process.nextTick(cb, Fuse.EFAULT);93 }94 });95 },96 open: (path, flags, cb) => {97 //console.log('I>open(%s, %d)', path, flags);98 fuseops99 .open(path, flags)100 .then(response => {101 //console.log('I>opened with fd',response)102 return process.nextTick(cb, 0, response);103 })104 .catch(err => {105 if (err instanceof FSError) {106 return process.nextTick(cb, err.errno);107 } else {108 return process.nextTick(cb, Fuse.EFAULT);109 }110 });111 },112 release: (path, fd, cb) => {113 //console.log('I>close(%s, %d)', path, fd);114 fuseops115 .close(path, fd)116 .then(() => {117 return process.nextTick(cb, 0);118 })119 .catch(err => {120 if (err instanceof FSError) {121 return process.nextTick(cb, err.errno);122 } else {123 return process.nextTick(cb, Fuse.EFAULT);124 }125 });126 },127 chmod: (path, mode, cb) => {128 //console.log('I>chmod(%s, %d)', path, mode);129 fuseops130 .chmod(path, mode)131 .then(() => {132 return process.nextTick(cb, 0);133 })134 .catch(err => {135 if (err instanceof FSError) {136 return process.nextTick(cb, err.errno);137 } else {138 return process.nextTick(cb, Fuse.EFAULT);139 }140 });141 },142 read: (path, fd, buf, len, pos, cb) => {143 console.log('I>read(%s,%d)', path, fd,len,pos);144 fuseops145 .read(path, fd, buf, len, pos)146 .then(e => {147 return process.nextTick(cb, e);148 })149 .catch(err => {150 if (err instanceof FSError) {151 return process.nextTick(cb, err.errno);152 } else {153 return process.nextTick(cb, Fuse.EFAULT);154 }155 });156 },157 create: (path, mode, cb) => {158 //console.log('I>create(%s,%d)', path, mode);159 fuseops160 .create(path, mode)161 .then((val) => {162 return process.nextTick(cb, 0,val);163 })164 .catch(err => {165 //console.log('Create threw an error')166 if (err instanceof FSError) {167 return process.nextTick(cb, err.errno);168 } else {169 return process.nextTick(cb, Fuse.EFAULT);170 }171 });172 // return process.nextTick(cb,Fuse.EHOSTUNREACH)173 },174 utimens: (path, atime, mtime, cb) => {175 //console.log('I>utime',path,atime,mtime);176 fuseops.utimens(path,atime,mtime).then(()=>{177 return cb(0);178 }).catch(err=>{179 if (err instanceof FSError) {180 return process.nextTick(cb, err.errno);181 } else {182 return process.nextTick(cb, Fuse.EFAULT);183 }184 })185 //dummy function186 // //console.log('I>changetimes(faking)', path, atime, mtime,typeof atime, typeof mtime);187 // return process.nextTick(cb, 0);188 },189 mkdir: (path, mode, cb) => {190 //console.log('I>mkdir(%s,%d)', path, mode);191 fuseops192 .mkdir(path, mode)193 .then(() => {194 return process.nextTick(cb, 0);195 })196 .catch(err => {197 if (err instanceof FSError) {198 return process.nextTick(cb, err.errno);199 } else {200 return process.nextTick(cb, Fuse.EFAULT);201 }202 });203 },204 rename: (src, dest, cb) => {205 //console.log('I>rename(%s,%s)', src, dest);206 fuseops207 .rename(src, dest)208 .then(() => {209 return process.nextTick(cb, 0);210 })211 .catch(err => {212 if (err instanceof FSError) {213 return process.nextTick(cb, err.errno);214 } else {215 return process.nextTick(cb, Fuse.EFAULT);216 }217 });218 },219 rmdir: (pathstr, cb) => {220 //console.log('I>rmdir(%s)', pathstr);221 fuseops222 .rmdir(pathstr)223 .then(() => {224 return process.nextTick(cb, 0);225 })226 .catch(err => {227 if (err instanceof FSError) {228 return process.nextTick(cb, err.errno);229 } else {230 return process.nextTick(cb, Fuse.EFAULT);231 }232 });233 },234 write: (path, fd, buffer, length, position, cb) => {235 /**236 * @type {Buffer}237 */238 const mybuf = buffer;239 //console.log('I>write',path, fd, length, position);240 fuseops241 .write(fd, buffer, length, position, cb)242 .then(res => {243 //console.log('wrote', res);244 return process.nextTick(cb, res);245 })246 .catch(err => {247 if (err instanceof FSError) {248 return process.nextTick(cb, err.errno);249 } else {250 return process.nextTick(cb, Fuse.EFAULT);251 }252 });253 // return process.nextTick(cb, 0);254 },255 unlink: (path, cb) => {256 // console.log('I>unlink', path);257 fuseops258 .unlink(path)259 .then(() => {260 return process.nextTick(cb, 0);261 })262 .catch(err => {263 if (err instanceof FSError) {264 return process.nextTick(cb, err.errno);265 } else {266 return process.nextTick(cb, Fuse.EFAULT);267 }268 });269 },270 flush: (path, fd, cb) => {271 //foobar272 // console.log('I>flush',path);273 return cb(0);274 },275 releasedir: (path, fd, cb) => {276 return cb(0);277 },278 opendir: (path, flags, cb) => {279 return cb(0, 42);280 },281 // getxattr: (path, name, position, cb) => {282 // //console.log('I>getxattr', path,name);283 // return cb(0, '');284 // },285 truncate: (path, size, cb) => {286 // console.log('I>Truncate', path, size);287 fuseops288 .truncate(path, size)289 .then(() => {290 return process.nextTick(cb, 0);291 })292 .catch(err => {293 if (err instanceof FSError) {294 return process.nextTick(cb, err.errno);295 } else {296 return process.nextTick(cb, Fuse.EFAULT);297 }298 });299 },300};301/**302 * Fuse handler303 */304const fuse = new Fuse(creds.directory, ops, {305 debug: process.env.COUSCOUS_FUSEDEBUG === 'true',306 displayFolder: true,307});308/**309 * Mount FUSE310 */311fuseops312 .init(creds.email,creds.pwd,creds.url)313 .then(() => {314 fuse.mount(err => {315 if (err) {316 throw err;317 }318 //console.log('FS mounted at ' + fuse.mnt);319 });320 })321 .catch(err => {322 if (err instanceof FSError) {323 //console.log('could not mount with error ', err.errno);324 process.exit(1);325 // return process.nextTick(cb, err.errno);326 } else {327 console.log('E>could not mount, general error:', err.message || err.name || err.msg || err);328 process.exit(1);329 // return process.nextTick(cb, Fuse.EFAULT);330 }331 });332/**333 * Unmount FUSE on SIGINT334 */335process.once('SIGINT', () => {336 fuse.unmount(err => {337 if (err) {338 console.log(339 '\nFS at ' + fuse.mnt + ' could not be unmounted: ' + err340 );341 return;342 }343 fuseops.deinit().then(() => {...
valid-next-tick.js
Source:valid-next-tick.js
...31 code: `<script>import { nextTick as nt } from 'vue';32 export default {33 async mounted() {34 await nt();35 await Vue.nextTick();36 await this.$nextTick();37 nt().then(callback);38 Vue.nextTick().then(callback);39 this.$nextTick().then(callback);40 nt(callback);41 Vue.nextTick(callback);42 this.$nextTick(callback);43 }44 }</script>`45 },46 // https://github.com/vuejs/eslint-plugin-vue/pull/1404#discussion_r55093750047 {48 filename: 'test.vue',49 code: `<script>import { nextTick as nt } from 'vue';50 export default {51 mounted() {52 foo.then(nt);53 foo.then(Vue.nextTick);54 foo.then(this.$nextTick);55 foo.then(nt, catchHandler);56 foo.then(Vue.nextTick, catchHandler);57 foo.then(this.$nextTick, catchHandler);58 }59 }</script>`60 },61 // https://github.com/vuejs/eslint-plugin-vue/pull/1404#discussion_r55093641062 {63 filename: 'test.vue',64 code: `<script>import { nextTick as nt } from 'vue';65 export default {66 mounted() {67 let foo = nt;68 foo = Vue.nextTick;69 foo = this.$nextTick;70 }71 }</script>`72 },73 // https://github.com/vuejs/eslint-plugin-vue/pull/1404#discussion_r55093693374 {75 filename: 'test.vue',76 code: `<script>import { nextTick as nt } from 'vue';77 export default {78 mounted() {79 Promise.all([nt(), someOtherPromise]);80 Promise.all([Vue.nextTick(), someOtherPromise]);81 Promise.all([this.$nextTick(), someOtherPromise]);82 }83 }</script>`84 },85 // https://github.com/vuejs/eslint-plugin-vue/pull/1404#discussion_r55176996986 {87 filename: 'test.vue',88 code: `<script>import { nextTick as nt } from 'vue';89 export default {90 created() {91 let queue = nt();92 queue = queue.then(nt);93 return nt();94 },95 mounted() {96 const queue = Vue.nextTick();97 return Vue.nextTick();98 },99 updated() {100 const queue = this.$nextTick();101 return this.$nextTick();102 }103 }</script>`104 },105 {106 filename: 'test.vue',107 code: `<script>;108 export default {109 methods: {110 fn1 () {111 return this.$nextTick()112 },113 fn2 () {114 return this.$nextTick()115 .then(() => this.$nextTick())116 },117 }118 }</script>`119 },120 // https://github.com/vuejs/eslint-plugin-vue/issues/1776121 {122 filename: 'test.vue',123 code: `<script>import { nextTick as nt } from 'vue';124 export default {125 mounted() {126 let foo = bar ? nt : undefined;127 foo = bar ? Vue.nextTick : undefined;128 foo = bar ? this.$nextTick : undefined;129 }130 }</script>`131 }132 ],133 invalid: [134 {135 filename: 'test.vue',136 code: `<script>import { nextTick as nt } from 'vue';137 export default {138 async mounted() {139 nt();140 Vue.nextTick();141 this.$nextTick();142 }143 }</script>`,144 output: null,145 errors: [146 {147 message:148 'Await the Promise returned by `nextTick` or pass a callback function.',149 line: 4,150 column: 11,151 suggestions: [152 {153 output: `<script>import { nextTick as nt } from 'vue';154 export default {155 async mounted() {156 await nt();157 Vue.nextTick();158 this.$nextTick();159 }160 }</script>`161 }162 ]163 },164 {165 message:166 'Await the Promise returned by `nextTick` or pass a callback function.',167 line: 5,168 column: 15,169 suggestions: [170 {171 output: `<script>import { nextTick as nt } from 'vue';172 export default {173 async mounted() {174 nt();175 await Vue.nextTick();176 this.$nextTick();177 }178 }</script>`179 }180 ]181 },182 {183 message:184 'Await the Promise returned by `nextTick` or pass a callback function.',185 line: 6,186 column: 16,187 suggestions: [188 {189 output: `<script>import { nextTick as nt } from 'vue';190 export default {191 async mounted() {192 nt();193 Vue.nextTick();194 await this.$nextTick();195 }196 }</script>`197 }198 ]199 }200 ]201 },202 {203 filename: 'test.vue',204 code: `<script>import { nextTick as nt } from 'vue';205 export default {206 mounted() {207 nt;208 Vue.nextTick;209 this.$nextTick;210 nt.then(callback);211 Vue.nextTick.then(callback);212 this.$nextTick.then(callback);213 }214 }</script>`,215 output: `<script>import { nextTick as nt } from 'vue';216 export default {217 mounted() {218 nt();219 Vue.nextTick();220 this.$nextTick();221 nt().then(callback);222 Vue.nextTick().then(callback);223 this.$nextTick().then(callback);224 }225 }</script>`,226 errors: [227 {228 message: '`nextTick` is a function.',229 line: 4,230 column: 11231 },232 {233 message: '`nextTick` is a function.',234 line: 5,235 column: 15236 },237 {238 message: '`nextTick` is a function.',239 line: 6,240 column: 16241 },242 {243 message: '`nextTick` is a function.',244 line: 8,245 column: 11246 },247 {248 message: '`nextTick` is a function.',249 line: 9,250 column: 15251 },252 {253 message: '`nextTick` is a function.',254 line: 10,255 column: 16256 }257 ]258 },259 {260 filename: 'test.vue',261 code: `<script>import { nextTick as nt } from 'vue';262 export default {263 async mounted() {264 await nt;265 await Vue.nextTick;266 return this.$nextTick;267 }268 }</script>`,269 output: `<script>import { nextTick as nt } from 'vue';270 export default {271 async mounted() {272 await nt();273 await Vue.nextTick();274 return this.$nextTick();275 }276 }</script>`,277 errors: [278 {279 message: '`nextTick` is a function.',280 line: 4,281 column: 17282 },283 {284 message: '`nextTick` is a function.',285 line: 5,286 column: 21287 },288 {289 message: '`nextTick` is a function.',290 line: 6,291 column: 23292 }293 ]294 },295 // https://github.com/vuejs/eslint-plugin-vue/pull/1404#discussion_r550936933296 {297 filename: 'test.vue',298 code: `<script>import { nextTick as nt } from 'vue';299 export default {300 mounted() {301 Promise.all([nt, someOtherPromise]);302 Promise.all([Vue.nextTick, someOtherPromise]);303 Promise.all([this.$nextTick, someOtherPromise]);304 }305 }</script>`,306 output: `<script>import { nextTick as nt } from 'vue';307 export default {308 mounted() {309 Promise.all([nt(), someOtherPromise]);310 Promise.all([Vue.nextTick(), someOtherPromise]);311 Promise.all([this.$nextTick(), someOtherPromise]);312 }313 }</script>`,314 errors: [315 {316 message: '`nextTick` is a function.',317 line: 4,318 column: 24319 },320 {321 message: '`nextTick` is a function.',322 line: 5,323 column: 28324 },325 {326 message: '`nextTick` is a function.',327 line: 6,328 column: 29329 }330 ]331 },332 {333 filename: 'test.vue',334 code: `<script>import { nextTick as nt } from 'vue';335 export default {336 mounted() {337 nt(callback, anotherCallback);338 Vue.nextTick(callback, anotherCallback);339 this.$nextTick(callback, anotherCallback);340 }341 }</script>`,342 output: null,343 errors: [344 {345 message: '`nextTick` expects zero or one parameters.',346 line: 4,347 column: 11348 },349 {350 message: '`nextTick` expects zero or one parameters.',351 line: 5,352 column: 15353 },354 {355 message: '`nextTick` expects zero or one parameters.',356 line: 6,357 column: 16358 }359 ]360 },361 {362 filename: 'test.vue',363 code: `<script>import { nextTick as nt } from 'vue';364 export default {365 async mounted() {366 nt(callback).then(anotherCallback);367 Vue.nextTick(callback).then(anotherCallback);368 this.$nextTick(callback).then(anotherCallback);369 await nt(callback);370 await Vue.nextTick(callback);371 await this.$nextTick(callback);372 }373 }</script>`,374 output: null,375 errors: [376 {377 message:378 'Either await the Promise or pass a callback function to `nextTick`.',379 line: 4,380 column: 11381 },382 {383 message:384 'Either await the Promise or pass a callback function to `nextTick`.',385 line: 5,...
next-tick-style.js
Source:next-tick-style.js
...30 filename: 'test.vue',31 code: `<script>import { nextTick as nt } from 'vue';32 export default {33 async mounted() {34 this.$nextTick().then(() => callback());35 Vue.nextTick().then(() => callback());36 nt().then(() => callback());37 await this.$nextTick(); callback();38 await Vue.nextTick(); callback();39 await nt(); callback();40 }41 }</script>`42 },43 {44 filename: 'test.vue',45 options: ['promise'],46 code: `<script>import { nextTick as nt } from 'vue';47 export default {48 async mounted() {49 this.$nextTick().then(() => callback());50 Vue.nextTick().then(() => callback());51 nt().then(() => callback());52 await this.$nextTick(); callback();53 await Vue.nextTick(); callback();54 await nt(); callback();55 }56 }</script>`57 },58 {59 filename: 'test.vue',60 options: ['callback'],61 code: `<script>import { nextTick as nt } from 'vue';62 export default {63 mounted() {64 this.$nextTick(() => callback());65 Vue.nextTick(() => callback());66 nt(() => callback());67 this.$nextTick(callback);68 Vue.nextTick(callback);69 nt(callback);70 }71 }</script>`72 },73 // https://github.com/vuejs/eslint-plugin-vue/pull/1400#discussion_r55093797774 {75 filename: 'test.vue',76 options: ['promise'],77 code: `<script>import { nextTick as nt } from 'vue';78 export default {79 mounted() {80 foo.then(this.$nextTick);81 foo.then(Vue.nextTick);82 foo.then(nt);83 foo.then(nt, catchHandler);84 foo.then(Vue.nextTick, catchHandler);85 foo.then(this.$nextTick, catchHandler);86 }87 }</script>`88 },89 {90 filename: 'test.vue',91 options: ['callback'],92 code: `<script>import { nextTick as nt } from 'vue';93 export default {94 mounted() {95 foo.then(this.$nextTick);96 foo.then(Vue.nextTick);97 foo.then(nt);98 foo.then(nt, catchHandler);99 foo.then(Vue.nextTick, catchHandler);100 foo.then(this.$nextTick, catchHandler);101 }102 }</script>`103 }104 ],105 invalid: [106 {107 filename: 'test.vue',108 code: `<script>import { nextTick as nt } from 'vue';109 export default {110 mounted() {111 this.$nextTick(() => callback());112 Vue.nextTick(() => callback());113 nt(() => callback());114 this.$nextTick(callback);115 Vue.nextTick(callback);116 nt(callback);117 }118 }</script>`,119 output: `<script>import { nextTick as nt } from 'vue';120 export default {121 mounted() {122 this.$nextTick().then(() => callback());123 Vue.nextTick().then(() => callback());124 nt().then(() => callback());125 this.$nextTick().then(callback);126 Vue.nextTick().then(callback);127 nt().then(callback);128 }129 }</script>`,130 errors: [131 {132 message:133 'Use the Promise returned by `nextTick` instead of passing a callback function.',134 line: 4,135 column: 16136 },137 {138 message:139 'Use the Promise returned by `nextTick` instead of passing a callback function.',140 line: 5,141 column: 15142 },143 {144 message:145 'Use the Promise returned by `nextTick` instead of passing a callback function.',146 line: 6,147 column: 11148 },149 {150 message:151 'Use the Promise returned by `nextTick` instead of passing a callback function.',152 line: 8,153 column: 16154 },155 {156 message:157 'Use the Promise returned by `nextTick` instead of passing a callback function.',158 line: 9,159 column: 15160 },161 {162 message:163 'Use the Promise returned by `nextTick` instead of passing a callback function.',164 line: 10,165 column: 11166 }167 ]168 },169 {170 filename: 'test.vue',171 options: ['promise'],172 code: `<script>import { nextTick as nt } from 'vue';173 export default {174 mounted() {175 this.$nextTick(() => callback());176 Vue.nextTick(() => callback());177 nt(() => callback());178 this.$nextTick(callback);179 Vue.nextTick(callback);180 nt(callback);181 }182 }</script>`,183 output: `<script>import { nextTick as nt } from 'vue';184 export default {185 mounted() {186 this.$nextTick().then(() => callback());187 Vue.nextTick().then(() => callback());188 nt().then(() => callback());189 this.$nextTick().then(callback);190 Vue.nextTick().then(callback);191 nt().then(callback);192 }193 }</script>`,194 errors: [195 {196 message:197 'Use the Promise returned by `nextTick` instead of passing a callback function.',198 line: 4,199 column: 16200 },201 {202 message:203 'Use the Promise returned by `nextTick` instead of passing a callback function.',204 line: 5,205 column: 15206 },207 {208 message:209 'Use the Promise returned by `nextTick` instead of passing a callback function.',210 line: 6,211 column: 11212 },213 {214 message:215 'Use the Promise returned by `nextTick` instead of passing a callback function.',216 line: 8,217 column: 16218 },219 {220 message:221 'Use the Promise returned by `nextTick` instead of passing a callback function.',222 line: 9,223 column: 15224 },225 {226 message:227 'Use the Promise returned by `nextTick` instead of passing a callback function.',228 line: 10,229 column: 11230 }231 ]232 },233 {234 filename: 'test.vue',235 options: ['callback'],236 code: `<script>import { nextTick as nt } from 'vue';237 export default {238 async mounted() {239 this.$nextTick().then(() => callback());240 Vue.nextTick().then(() => callback());241 nt().then(() => callback());242 await this.$nextTick(); callback();243 await Vue.nextTick(); callback();244 await nt(); callback();245 }246 }</script>`,247 output: null,248 errors: [249 {250 message:251 'Pass a callback function to `nextTick` instead of using the returned Promise.',252 line: 4,253 column: 16254 },255 {256 message:257 'Pass a callback function to `nextTick` instead of using the returned Promise.',...
asyncize.js
Source:asyncize.js
...62 }63 if (arguments.length === 1 && !isFunc(arguments[0])) {//must be expecting a promise and are passing in an object param64 return new Promise((resolve, reject) => {65 if (success) {66 process.nextTick(resolve, success);67 }68 else if (typeof queriableData !== 'undefined') {69 let result = module.exports.queryData(queriableData, arguments[0]);70 if(result !== null){71 process.nextTick(resolve, result);72 }73 else {74 process.nextTick(reject, error);75 }76 }77 else {78 process.nextTick(reject, error);79 }80 });81 }82 else if (arguments.length === 1 && isFunc(arguments[0])) {//must be passing in a singleton 'nodejs' error first callback83 if (success) {84 process.nextTick(arguments[0], null, success);85 }86 else {87 process.nextTick(arguments[0], error);88 }89 }90 else if (arguments.length === 2 && !isFunc(arguments[0]) && isFunc(arguments[1])) {//you must be passing in a param obj, as well as a error first callback91 if (success) {92 process.nextTick(arguments[1], null, success);93 }94 else if (queriableData) {95 let result = module.exports.queryData(queriableData, arguments[0]);96 if(result !== null){97 process.nextTick(arguments[1], null, result);98 }99 else {100 process.nextTick(arguments[1], error);101 }102 }103 else {104 process.nextTick(arguments[1], error);105 }106 }107 else if (arguments.length === 2 && isFunc(arguments[0]) && isFunc(arguments[1])) {//you must be passing both ok,fail callbacks108 if (success) {109 process.nextTick(arguments[0], success);110 }111 else {112 process.nextTick(arguments[1], error);113 }114 }115 else if ((arguments.length > 2) && !isFunc(arguments[0]) && isFunc(arguments[1]) && isFunc(arguments[2])) {// ok and fail as well as a object param116 if (success) {117 process.nextTick(arguments[1], success);118 }119 else if (queriableData) {120 let result = module.exports.queryData(queriableData, arguments[0]);121 if(result !== null){122 process.nextTick(arguments[1], module.exports.queryData(queriableData, arguments[0]));123 }124 else {125 process.nextTick(arguments[2], error);126 }127 }128 else {129 process.nextTick(arguments[2], error);130 }131 }132 else {// length === 0 or some other situation133 return new Promise((resolve, reject) => {134 if (success) {135 process.nextTick(resolve, success);136 }137 else if (queriableData) {138 process.nextTick(reject, queriableData);139 }140 else {141 process.nextTick(reject, error);142 }143 });144 }145 };146 },147 /**148 * Asynchronizes sync functions and standalone values turning them into a149 * err first callback or a ok/fail callback pair, or a promise150 *151 * @param value152 * @param args153 * @returns {Function}154 */155 async: function (value, args) {156 return function callbackable(ok, fail) {157 return new Promise((resolve, reject) => {158 let aok = module.exports.spreader(ok, resolve);159 let nope = module.exports.spreader(fail, reject);160 if (typeof value === 'function') {161 try {162 process.nextTick(aok, value.apply(self, args));163 }164 catch (e) {165 if(typeof fail !== 'function' && typeof ok === 'function') {166 process.nextTick(ok, null, e);167 process.nextTick(reject, e);168 }169 else {170 process.nextTick(nope, e);171 }172 }173 return;174 }175 else if(typeof value === 'undefined'){//error condition176 //incases were the client wants to user error first callbacks177 if(typeof fail !== 'function' && typeof ok === 'function') {178 process.nextTick(ok, null, args);179 process.nextTick(reject, args);180 }181 else {182 process.nextTick(nope, args);183 }184 return;185 }186 aok(value);187 });188 };189 }...
test.js
Source:test.js
...72 throw new Error('Did not call the callback immediately after the first error');73 }74 done();75 }, true);76 process.nextTick(callbackManager.registerCallback());77 process.nextTick(callbackManager.registerCallback().bind(null, firstError));78 process.nextTick(callbackManager.registerCallback().bind(null, otherError));79 process.nextTick(callbackManager.registerCallback());80 process.nextTick(callbackManager.registerCallback().bind(null, otherError));81 });82 it('should not invoke the callback with a value if a non-error value was encountered', done => {83 var callbackManager = new CallbackManager(function(err) {84 if (err) {85 throw new Error('Should not have been invoked with an error value');86 }87 done();88 });89 process.nextTick(callbackManager.registerCallback());90 process.nextTick(callbackManager.registerCallback().bind(null, true));91 process.nextTick(callbackManager.registerCallback().bind(null, 10));92 process.nextTick(callbackManager.registerCallback());93 process.nextTick(callbackManager.registerCallback().bind(null, 'error'));94 });95 it('should be reusable, even after an error', done => {96 var expectedErrValue = new Error();97 var callbackManager = new CallbackManager(function(err) {98 if (err !== expectedErrValue) {99 throw new Error('Unexpected err value: ' + err);100 }101 if (err) {102 expectedErrValue = null;103 process.nextTick(callbackManager.registerCallback());104 process.nextTick(callbackManager.registerCallback());105 } else {106 done();107 }108 });109 process.nextTick(callbackManager.registerCallback());110 process.nextTick(callbackManager.registerCallback().bind(null, expectedErrValue));111 process.nextTick(callbackManager.registerCallback());112 });113 describe('#callback', () => {114 it('should be the original callback passed to the constructor', () => {115 function callback() {}116 var callbackManager = new CallbackManager(callback);117 callbackManager.callback.should.equal(callback);118 });119 it('should not be modifiable', () => {120 function callback() {}121 var callbackManager = new CallbackManager(callback);122 (function() {123 callbackManager.callback = 1;124 }).should.throw(TypeError);125 });126 });127 describe('#registerCallback()', () => {128 it('should return a function', () => {129 var callbackManager = new CallbackManager(function() {});130 callbackManager.registerCallback().should.be.a.Function();131 });132 });133 describe('#getCount()', () => {134 it('should return the number of callbacks currently waiting to be called', done => {135 var callbackManager = new CallbackManager(done);136 callbackManager.getCount().should.equal(0);137 process.nextTick(callbackManager.registerCallback());138 callbackManager.getCount().should.equal(1);139 process.nextTick(callbackManager.registerCallback());140 process.nextTick(() => callbackManager.getCount().should.equal(1));141 process.nextTick(callbackManager.registerCallback());142 callbackManager.getCount().should.equal(3);143 });144 });145 describe('#abort()', () => {146 it('should prevent the provided callback from being called', done => {147 var callbackManager = new CallbackManager(function() {148 throw new Error('Callback not aborted');149 });150 process.nextTick(callbackManager.registerCallback());151 process.nextTick(() => callbackManager.abort());152 process.nextTick(callbackManager.registerCallback());153 process.nextTick(callbackManager.registerCallback());154 process.nextTick(done);155 });156 });...
example10.js
Source:example10.js
...19const __mapEventLoop = true;20const intervalTimeout = setInterval(() => {21 logger.debug('TIMERS PHASE: GREETINGS', 'setInterval');22 if (__mapEventLoop) {23 process.nextTick(() => {24 logger.debug('TIMERS PHASE DONE', 'ReadableStream.on(open).callback(process.nextTick)');25 });26 }27}, 0);28readableStream.on('open', (fd) => {29 logger.info('open event received', 'ReadableStream.on(open).callback');30 if (__mapEventLoop) {31 process.nextTick(() => {32 logger.debug('POLL PHASE DONE', 'ReadableStream.on(open).callback(process.nextTick)');33 });34 // The next two calls prove where the ready event is coming from35 setTimeout(() => {36 logger.debug('TIMER EXPIRED', 'ReadableStream.on(ready).callback(setTimeout)');37 process.nextTick(() => {38 logger.debug('TIMERS PHASE DONE', 'ReadableStream.on(ready).callback(setTimeout.process.nextTick)');39 });40 }, 0);41 setImmediate(() => {42 logger.debug('CHECK PHASE: GREETINGS', 'ReadableStream.on(ready).callback(setImmediate)');43 process.nextTick(() => {44 logger.debug('CHECK PHASE DONE', 'ReadableStream.on(ready).callback(setImmediate.process.nextTick)');45 });46 });47 // Eat some CPU to make sure the setTimeout(0) has expired before we let the event loop continue48 simpleUtils.eatCpu(3);49 }50});51readableStream.on('data', (chunk) => {52 logger.info('data event received: chunk size: ' + chunk.length, 'ReadableStream.on(data).callback');53 if (__mapEventLoop) {54 process.nextTick(() => {55 logger.debug('POLL PHASE DONE', 'ReadableStream.on(data).callback(process.nextTick)');56 });57 setTimeout(() => {58 logger.debug('TIMER EXPIRED', 'ReadableStream.on(data).callback(setTimeout)');59 process.nextTick(() => {60 logger.debug('TIMERS PHASE DONE', 'ReadableStream.on(data).callback(setTimeout.process.nextTick)');61 });62 }, 0);63 setImmediate(() => {64 logger.debug('CHECK PHASE: GREETINGS', 'ReadableStream.on(data).callback(setImmediate)');65 process.nextTick(() => {66 logger.debug('CHECK PHASE DONE', 'ReadableStream.on(data).callback(setImmediate.process.nextTick)');67 });68 });69 }70});71readableStream.on('close', () => {72 logger.info('close event received', 'ReadableStream.on(close).callback');73 if (__mapEventLoop) {74 process.nextTick(() => {75 logger.debug('POLL PHASE DONE', 'ReadableStream.on(close).callback(process.nextTick)');76 });77 // The next two calls prove where the close event is coming from78 // If we see the setTimeout(0) callback next, then the close event is coming from the close phase79 setTimeout(() => {80 logger.debug('TIMER EXPIRED', 'ReadableStream.on(close).callback(setTimeout)');81 process.nextTick(() => {82 logger.debug('TIMERS PHASE DONE', 'ReadableStream.on(close).callback(setTimeout.process.nextTick)');83 });84 }, 0);85 // If we see the setImmediate() callback next, then close event is coming from the poll phase86 setImmediate(() => {87 logger.debug('CHECK PHASE: GREETINGS', 'ReadableStream.on(close).callback(setImmediate)');88 process.nextTick(() => {89 logger.debug('CHECK PHASE DONE', 'ReadableStream.on(close).callback(setImmediate.process.nextTick)');90 });91 });92 // Eat some CPU to make sure the setTimeout(0) has expired before we let the event loop continue93 simpleUtils.eatCpu(3);94 }95 clearInterval(intervalTimeout);96});97readableStream.on('error', (error) => {98 logger.info('ERROR: ' + error);99 if (__mapEventLoop) {100 process.nextTick(() => {101 logger.debug('POLL PHASE DONE', 'ReadableStream.on(error).callback(process.nextTick)');102 });103 // The next two calls prove where the error event is coming from104 // If we see the setTimeout(0) callback next, then the error event is coming from the close phase105 setTimeout(() => {106 logger.debug('TIMER EXPIRED', 'ReadableStream.on(error).callback(setTimeout)');107 process.nextTick(() => {108 logger.debug('TIMERS PHASE DONE', 'ReadableStream.on(error).callback(setTimeout.process.nextTick)');109 });110 }, 0);111 // If we see the setImmediate() callback next, then error event is coming from the poll phase112 setImmediate(() => {113 logger.debug('CHECK PHASE: GREETINGS', 'ReadableStream.on(error).callback(setImmediate)');114 process.nextTick(() => {115 logger.debug('CHECK PHASE DONE', 'ReadableStream.on(error).callback(setImmediate.process.nextTick)');116 });117 });118 // Eat some CPU to make sure the setTimeout(0) has expired before we let the event loop continue119 simpleUtils.eatCpu(3);120 }121 clearInterval(intervalTimeout);...
02-非异步IO的API.js
Source:02-非异步IO的API.js
2 * éè¦å
³æ³¨çAPI3 * > setTimeout()4 * > setInterval()5 * > setImmediate()6 * > process.nextTick()7 */8/**9 * å®æ¶å¨10 * setTimeout() å setInterval() ä¸æµè§å¨ä¸çAPIæ¯ä¸è´ç,åå«ç¨äºå次åå¤æ¬¡å®æ¶æ§è¡ä»»å¡.11 * è°ç¨setTimeout() æ setInterval() å建çå®æ¶å¨ä¼è¢«æå
¥å°å®æ¶å¨è§å¯è
å
é¨çä¸ä¸ªé»çº¢æ ä¸.12 * æ¯æ¬¡Tickæ§è¡æ¶,ä¼ä»è¯¥çº¢é»æ ä¸è¿ä»£ååºå®æ¶å¨å¯¹è±¡,æ£æ¥æ¯å¦è¶
è¿å®æ¶æ¶é´,å¦æè¶
æ¶,就形æä¸ä¸ªäºä»¶,13 * å®çåè°å½æ°å°ç«å³æ§è¡.14 */15/**16 * process.nextTick()17 * æ¯æ¬¡è°ç¨ process.nextTick()æ¹æ³, åªä¼å°åè°å½æ°æ¾å
¥éåä¸, å¨ä¸ä¸è½®Tickæ¶ååºæ§è¡. å®æ¶å¨ä¸18 * éç¨çº¢é»æ çæä½æ¶é´å¤æ度为 O(lg(n)), nextTick() çæ¶é´å¤æ度为 O(1).ç¸äº¤ä¹ä¸, process.nextTick()æ´ä¸ºé«æ.19 */20/**21 * setImmediate()22 * å®ä¸process.nextTick()æ¹æ³åå类似,é½æ¯å°åè°å½æ°å»¶è¿æ§è¡. å¨ Node v0.9.1 ä¹å, setImmediate()23 * è¿æ²¡æå®ç°,é£æ¶åå®ç°ç±»ä¼¼çåè½ä¸»è¦æ¯éè¿ process.nextTick() æ¥å®æ, 该æ¹æ³ç代ç å¦ä¸24 */25// process.nextTick(function(){26// console.log('process 延è¿æ§è¡');27// });28// console.log('process æ£å¸¸æ§è¡');29// setImmediate(function(){30// console.log('Immediate 延è¿æ§è¡');31// });32// console.log('Immediate æ£å¸¸æ§è¡');33/**34 * setImmediate() å process.nextTick() ä¹é´æ¯æç»å¾®å·®å«ç.ä»ä»¥ä¸ç»æçåº35 * > process.nextTick() ä¸çåè°å½æ°æ§è¡çä¼å
级 è¦é«äº setImmediate(). è¿éçåå å¨äºäºä»¶å¾ªç¯å¯¹è§å¯è
çæ£æ¥æ¯æå
å顺åºç.36 * process.nextTick() å±äº idle è§å¯è
, setImmediate() å±äº check è§å¯è
.37 * 38 * > å¨æ¯ä¸æ¬¡è½®è¯¢æ£æ¥ä¸, idle è§å¯è
å
äº I/O è§å¯è
, I/O è§å¯è
å
äº check è§å¯è
.39 * 40 * > å¨å
·ä½åºå±å®ç°ä¸, process.nextTick() çåè°å½æ°ä¿åå¨ä¸ä¸ªæ°ç»ä¸, setImmediate() çç»æåä¿åå¨é¾è¡¨ä¸.41 * > å¨è¡ä¸ºä¸, process.nextTick()å¨æ¯è½®å¾ªç¯ä¸ä¼å°æ°ç»ä¸çåè°å½æ°å
¨é¨æ§è¡å®, è setImmediate()å¨æ¯è½®å¾ªç¯ä¸42 * æ§è¡é¾è¡¨ä¸çä¸ä¸ªåè°å½æ°. ç以ä¸å®ä¾.43 */44// å å
¥ä¸¤ä¸ª nextTick()çåè°å½æ°45process.nextTick(() =>{46 console.log('nextTick延è¿æ§è¡1');47});48process.nextTick(() =>{49 console.log('nextTick延è¿æ§è¡2');50});51//å å
¥ä¸¤ä¸ªsetImmediate()çåè°å½æ°52setImmediate(()=> {53 console.log('setImmediate延è¿æ§è¡1');54 //è¿å
¥ä¸æ¬¡å¾ªç¯55 process.nextTick(() => {56 console.log('强å¿æå
¥');57 });58});59setImmediate(() =>{60 console.log('setImmediate延è¿æ§è¡2');61});62console.log('æ£å¸¸æ§è¡');63/**64 * ä»æ§è¡ç»æä¸æ¥ç, å½ç¬¬ä¸ä¸ªsetImmediate() çåè°å½æ°æ§è¡å, 并没æç«å³æ§è¡ç¬¬äºä¸ª,èæ¯è¿å
¥äºä¸ä¸è½®å¾ªç¯,å次æç
§65 * > process.nextTick()ä¼å
, setImmediate()次åç顺åºæ§è¡.66 * ä¹æ以è¿æ ·è®¾è®¡, æ¯ä¸ºäºä¿è¯æ¯è½®å¾ªç¯è½å¤è¾å¿«å°æ§è¡ç»æ,é²æ¢CPUå ç¨è¿å¤èé»å¡åç»I/Oè°ç¨çæ
åµ....
next-tick-depth-args.js
Source:next-tick-depth-args.js
...8 var n = +conf.millions * 1e6;9 function cb4(arg1, arg2, arg3, arg4) {10 if (--n) {11 if (n % 4 === 0)12 process.nextTick(cb4, 3.14, 1024, true, false);13 else if (n % 3 === 0)14 process.nextTick(cb3, 512, true, null);15 else if (n % 2 === 0)16 process.nextTick(cb2, false, 5.1);17 else18 process.nextTick(cb1, 0);19 } else20 bench.end(+conf.millions);21 }22 function cb3(arg1, arg2, arg3) {23 if (--n) {24 if (n % 4 === 0)25 process.nextTick(cb4, 3.14, 1024, true, false);26 else if (n % 3 === 0)27 process.nextTick(cb3, 512, true, null);28 else if (n % 2 === 0)29 process.nextTick(cb2, false, 5.1);30 else31 process.nextTick(cb1, 0);32 } else33 bench.end(+conf.millions);34 }35 function cb2(arg1, arg2) {36 if (--n) {37 if (n % 4 === 0)38 process.nextTick(cb4, 3.14, 1024, true, false);39 else if (n % 3 === 0)40 process.nextTick(cb3, 512, true, null);41 else if (n % 2 === 0)42 process.nextTick(cb2, false, 5.1);43 else44 process.nextTick(cb1, 0);45 } else46 bench.end(+conf.millions);47 }48 function cb1(arg1) {49 if (--n) {50 if (n % 4 === 0)51 process.nextTick(cb4, 3.14, 1024, true, false);52 else if (n % 3 === 0)53 process.nextTick(cb3, 512, true, null);54 else if (n % 2 === 0)55 process.nextTick(cb2, false, 5.1);56 else57 process.nextTick(cb1, 0);58 } else59 bench.end(+conf.millions);60 }61 bench.start();62 process.nextTick(cb1, true);...
Using AI Code Generation
1var sinon = require('sinon');2var chai = require('chai');3var expect = chai.expect;4var assert = chai.assert;5var should = chai.should();6var sinonChai = require('sinon-chai');7chai.use(sinonChai);8var foo = {9 setBar: function (value) {10 this.bar = value;11 }12};13describe('sinon test', function () {14 it('should call the callback asynchronously', function () {15 var callback = sinon.spy();16 foo.setBar(123);17 foo.setBar(456, callback);18 assert(callback.called);19 });20 it('should call the callback asynchronously using nextTick', function () {21 var callback = sinon.spy();22 foo.setBar(123);23 foo.setBar(456, callback);24 sinon.nextTick(function () {25 assert(callback.called);26 });27 });28});29var sinon = require('sinon');30var chai = require('chai');31var expect = chai.expect;32var assert = chai.assert;33var should = chai.should();34var sinonChai = require('sinon-chai');35chai.use(sinonChai);36var foo = {37 setBar: function (value) {38 this.bar = value;39 }40};41describe('sinon test', function () {42 it('should call the callback asynchronously', function () {43 var callback = sinon.spy();44 foo.setBar(123);45 foo.setBar(456, callback);46 assert(callback.called);47 });48 it('should call the callback asynchronously using nextTick', function () {49 var clock = sinon.useFakeTimers();50 var callback = sinon.spy();51 foo.setBar(123);52 foo.setBar(456, callback);53 clock.tick(100);54 assert(callback.called);55 clock.restore();56 });57});58var sinon = require('sinon');59var chai = require('chai');60var expect = chai.expect;61var assert = chai.assert;62var should = chai.should();63var sinonChai = require('sinon-chai');64chai.use(sinonChai);65var foo = {66 setBar: function (value) {67 this.bar = value;68 }69};70describe('sinon test', function () {71 it('should call the callback asynchronously', function () {
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3var fs = require('fs');4var fsMock = sinon.mock(fs);5fsMock.expects('readFile').once().withArgs('file.txt').yields(null, 'data');6fs.readFile('file.txt', function(err, data) {7 assert.equal(data, 'data');8});9fsMock.verify();10var sinon = require('sinon');11var assert = require('assert');12var fs = require('fs');13var fsMock = sinon.mock(fs);14fsMock.expects('readFile').once().withArgs('file.txt').yields(null, 'data');15fs.readFile('file.txt', function(err, data) {16 assert.equal(data, 'data');17});18fsMock.verify();19var sinon = require('sinon');20var assert = require('assert');21var fs = require('fs');22var fsMock = sinon.mock(fs);23fsMock.expects('readFile').once().withArgs('file.txt').yields(null, 'data');24fs.readFile('file.txt', function(err, data) {25 assert.equal(data, 'data');26});27fsMock.verify();28var sinon = require('sinon');29var assert = require('assert');30var fs = require('fs');31var fsMock = sinon.mock(fs);32fsMock.expects('readFile').once().withArgs('file.txt').yields(null, 'data');33fs.readFile('file.txt', function(err, data) {34 assert.equal(data, 'data');35});36fsMock.verify();37var sinon = require('sinon');38var assert = require('assert');39var fs = require('fs');40var fsMock = sinon.mock(fs);41fsMock.expects('readFile').once().withArgs('file.txt').yields(null, 'data');42fs.readFile('file.txt', function(err, data) {43 assert.equal(data, 'data');44});45fsMock.verify();46var sinon = require('sinon');47var assert = require('assert');48var fs = require('fs');49var fsMock = sinon.mock(fs);50fsMock.expects('readFile').once().withArgs('file.txt').yields(null, 'data');51fs.readFile('file.txt',
Using AI Code Generation
1var sinon = require('sinon');2var someObject = {3 doSomething: function() {4 }5};6var spy = sinon.spy(someObject, 'doSomething');7someObject.doSomething();8var clock = sinon.useFakeTimers();9someObject.doSomething();10clock.tick(1000);
Using AI Code Generation
1var assert = require('assert');2var sinon = require('sinon');3var spy = sinon.spy();4var stub = sinon.stub().returns(42);5var obj = {6 method: function() {7 spy();8 stub();9 }10};11obj.method();12assert(spy.called);13assert(stub.called);14var assert = require('assert');15var sinon = require('sinon');16var spy = sinon.spy();17var stub = sinon.stub().returns(42);18var obj = {19 method: function() {20 spy();21 stub();22 }23};24obj.method();25assert(spy.called);26assert(stub.called);27var assert = require('assert');28var sinon = require('sinon');29var spy = sinon.spy();30var stub = sinon.stub().returns(42);31var obj = {32 method: function() {33 spy();34 stub();35 }36};37obj.method();38assert(spy.called);39assert(stub.called);40var assert = require('assert');41var sinon = require('sinon');42var spy = sinon.spy();43var stub = sinon.stub().returns(42);44var obj = {45 method: function() {46 spy();47 stub();48 }49};50obj.method();51assert(spy.called);52assert(stub.called);53var assert = require('assert');54var sinon = require('sinon');55var spy = sinon.spy();56var stub = sinon.stub().returns(42);57var obj = {58 method: function() {59 spy();60 stub();61 }62};63obj.method();64assert(spy.called);65assert(stub.called);66var assert = require('assert');67var sinon = require('sinon');68var spy = sinon.spy();69var stub = sinon.stub().returns(42);70var obj = {71 method: function() {72 spy();73 stub();74 }75};76obj.method();77assert(spy.called);78assert(stub.called);79var assert = require('assert');80var sinon = require('sinon');81var spy = sinon.spy();
Using AI Code Generation
1var sinon = require('sinon');2var myObject = {3 myMethod: function () { }4};5var spy = sinon.spy(myObject, "myMethod");6myObject.myMethod();7spy.restore();8var sinon = require('sinon');9var myObject = {10 myMethod: function () { }11};12var spy = sinon.spy(myObject, "myMethod");13myObject.myMethod();14spy.restore();15var sinon = require('sinon');16var myObject = {17 myMethod: function () { }18};19var spy = sinon.spy(myObject, "myMethod");20myObject.myMethod();21spy.restore();22var sinon = require('sinon');23var myObject = {24 myMethod: function () { }25};26var spy = sinon.spy(myObject, "myMethod");27myObject.myMethod();28spy.restore();29var sinon = require('sinon');30var myObject = {31 myMethod: function () { }32};33var spy = sinon.spy(myObject, "myMethod");34myObject.myMethod();35spy.restore();36var sinon = require('sinon');37var myObject = {38 myMethod: function () { }39};40var spy = sinon.spy(myObject, "myMethod");41myObject.myMethod();42spy.restore();43var sinon = require('sinon');44var myObject = {45 myMethod: function () { }46};47var spy = sinon.spy(myObject, "myMethod");48myObject.myMethod();49spy.restore();50var sinon = require('sinon');51var myObject = {52 myMethod: function () { }53};54var spy = sinon.spy(myObject, "myMethod");
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3var obj = {4 method: function() {5 console.log('called');6 }7};8var spy = sinon.spy(obj, 'method');9obj.method();10obj.method();11obj.method();12spy.restore();13obj.method();14var sinon = require('sinon');15var assert = require('assert');16var obj = {17 method: function() {18 console.log('called');19 }20};21var spy = sinon.spy(obj, 'method');22obj.method();23obj.method();24obj.method();25spy.restore();26obj.method();27var spy = sinon.spy();28setTimeout(spy, 100);29setTimeout(spy, 100);30setTimeout(spy, 100);31setTimeout(function() {32}, 200);33var stub = sinon.stub();34stub.withArgs(42).returns(1);35stub.withArgs(1).throws('TypeError');36var stub = sinon.stub();37stub.withArgs(42).returns(1);38stub.withArgs(1).throws('TypeError');39var stub = sinon.stub();40stub.withArgs(42).returns(1);41stub.withArgs(1).throws('TypeError');42var stub = sinon.stub();43stub.withArgs(42).returns(1);44stub.withArgs(1).throws('TypeError');45var stub = sinon.stub();46stub.withArgs(42).returns(1);47stub.withArgs(1).throws('TypeError');48var stub = sinon.stub();49stub.withArgs(42).returns(1);50stub.withArgs(1).throws('TypeError');51var stub = sinon.stub();52stub.withArgs(42).returns(1);53stub.withArgs(1
Using AI Code Generation
1var sinon = require('sinon');2var clock = sinon.useFakeTimers();3function test(){4 console.log("test");5}6setTimeout(test, 1000);7clock.tick(1000);
Using AI Code Generation
1it('should call the callback after 2 seconds', function(done) {2 this.timeout(3000);3 var clock = sinon.useFakeTimers();4 var callback = sinon.spy();5 asyncFunction(callback);6 clock.tick(2000);7 expect(callback).to.have.been.called;8 clock.restore();9 done();10});11it('should call the callback after 2 seconds', function(done) {12 this.timeout(3000);13 var clock = sinon.useFakeTimers();14 var callback = sinon.spy();15 asyncFunction(callback);16 setTimeout(function() {17 expect(callback).to.have.been.called;18 clock.restore();19 done();20 }, 2000);21});22sinon.useFakeTimers();23sinon.restore();
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3var test = require('./test.js');4describe('test', function(){5 it('should call the callback', function(){6 var callback = sinon.spy();7 test(callback);8 sinon.nextTick(function(){9 assert(callback.called);10 });11 });12});13process.nextTick(function(){14});15var assert = require('assert');16var test = require('./test.js');17describe('test', function(){18 it('should call the callback', function(done){19 var callback = function(){20 done();21 };22 test(callback);23 process.nextTick(function(){24 assert(callback.called);25 });26 });27});
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!