UNPKG

2.43 kBJavaScriptView Raw
1// awesome tests here!
2var test = require('tape'); // the reliable testing framework
3var setup = require('../setup'); // ensure decache is pre-loaded
4var decache = require('../decache.js');
5var mymodule = require('../lib/mymodule');
6
7console.log(mymodule.count);
8
9test('Expect decache to do nothing if the module name does not exist', function(t) {
10
11 try {
12 decache('./non-existing-module');
13 t.pass('No error thrown');
14 t.end();
15 } catch (e) {
16 t.fail('This should have not throw an error');
17 t.end();
18 }
19});
20
21test('Expect mymodule.count initial state to be false', function(t) {
22 t.equal(mymodule.get(), false, 'count is false! (we have not run this)');
23 t.end();
24});
25
26test('Increment the value of the count so its 1 (one)', function(t) {
27 var runcount = mymodule.set();
28 t.equal(runcount, 1, 'runcount is one! (as expected)');
29 t.end();
30});
31
32test('Increment the value of the count so its 2 (one)', function(t) {
33 mymodule.set();
34 var runcount = mymodule.get();
35 t.equal(runcount, 2, 'runcount is 2!');
36 t.end();
37});
38
39test('There\'s no going back to initial (runcount) state!', function(t) {
40 var runcount = mymodule.get();
41 t.equal(runcount, 2, 'runcount cannot be decremented!!');
42 t.end();
43});
44
45test('Delete Require Cache for mymodule to re-set the runcount!', function(t) {
46 decache('../lib/mymodule'); // exercise the decache module
47 var other = require('../lib/othermodule.js');
48 decache('../lib/othermodule.js');
49 mymodule = require('../lib/mymodule');
50 var runcount = mymodule.get();
51 t.equal(runcount, false, 'runcount is false! (as epxected)');
52 t.end();
53});
54
55test('Require an npm (non local) module', function(t) {
56 var ts = require('tap-spec');
57 decache('tap-spec');
58 var keys = Object.keys(require.cache);
59 t.equal(keys.indexOf('tap-spec'), -1, 'tap-spec no longer in require-cache');
60 t.end();
61});
62
63test('Fake relative parent module', function(t) {
64 var keys = Object.keys(require.cache);
65 var p = keys[0]; // the module that required decache
66 var obj = require.cache[p];
67 console.log(" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
68 require.cache = {};
69 require.cache[__filename] = obj;
70 console.log(require.cache);
71 var other = require('../lib/othermodule.js');
72 decache('../lib/othermodule.js');
73 keys = Object.keys(require.cache);
74 t.equal(keys.indexOf('othermodule.js'), -1, 'fake parent not in require.cache');
75 t.end();
76});