From ef589a6b133ac67a7904f8bb2cbec42c96dec914 Mon Sep 17 00:00:00 2001 From: Samy Pesse Date: Wed, 11 May 2016 13:02:20 +0200 Subject: [PATCH] Switch tests to mocha while keeping jest structure --- jest/customMatchers.js | 25 ---------- lib/__tests__/init.js | 2 +- lib/fs/__tests__/mock.js | 19 ++++--- lib/models/__tests__/config.js | 5 +- lib/models/__tests__/glossary.js | 10 ++-- lib/models/__tests__/glossaryEntry.js | 4 +- lib/models/__tests__/pluginDependency.js | 19 ++++++- lib/models/__tests__/summary.js | 2 +- lib/models/__tests__/templateBlock.js | 14 +++--- lib/models/config.js | 16 +++++- lib/models/pluginDependency.js | 28 +++++++++-- lib/modifiers/config/__tests__/addPlugin.js | 35 +++++++++++++ lib/modifiers/config/addPlugin.js | 5 +- lib/output/__tests__/ebook.js | 2 +- lib/output/__tests__/json.js | 4 +- lib/output/__tests__/website.js | 20 ++++---- .../modifiers/__tests__/addHeadingId.js | 9 ++-- .../modifiers/__tests__/annotateText.js | 5 +- .../modifiers/__tests__/fetchRemoteImages.js | 4 +- .../modifiers/__tests__/highlightCode.js | 13 ++--- lib/output/modifiers/__tests__/inlinePng.js | 2 +- .../modifiers/__tests__/resolveLinks.js | 13 ++--- lib/output/modifiers/__tests__/svgToImg.js | 2 +- lib/output/modifiers/__tests__/svgToPng.js | 7 +-- lib/parse/__tests__/listAssets.js | 2 +- lib/parse/__tests__/parseBook.js | 4 +- lib/parse/__tests__/parseGlossary.js | 4 +- lib/parse/__tests__/parseIgnore.js | 6 +-- lib/parse/__tests__/parseReadme.js | 4 +- lib/parse/__tests__/parseSummary.js | 4 +- lib/plugins/__tests__/findInstalled.js | 2 +- lib/plugins/__tests__/validatePlugin.js | 9 +--- lib/templating/__tests__/conrefsLoader.js | 6 +-- lib/utils/__tests__/git.js | 6 +-- lib/utils/__tests__/location.js | 4 +- package.json | 16 ++---- testing/setup.js | 49 +++++++++++++++++++ 37 files changed, 226 insertions(+), 155 deletions(-) delete mode 100644 jest/customMatchers.js create mode 100644 lib/modifiers/config/__tests__/addPlugin.js create mode 100644 testing/setup.js diff --git a/jest/customMatchers.js b/jest/customMatchers.js deleted file mode 100644 index 4dd4f68e6..000000000 --- a/jest/customMatchers.js +++ /dev/null @@ -1,25 +0,0 @@ -var path = require('path'); -var fs = require('fs'); - -var matchers = { - /** - Verify that a file exists in a directory - */ - toHaveFile: function () { - return { - compare: function (actual, expected) { - var filePath = path.join(actual, expected); - var exists = fs.existsSync(filePath); - - return { - pass: exists - }; - } - }; - } -}; - -jasmine.getEnv().beforeEach(function () { - jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000; - jasmine.addMatchers(matchers); -}); \ No newline at end of file diff --git a/lib/__tests__/init.js b/lib/__tests__/init.js index 5665cf1e0..66188a3a3 100644 --- a/lib/__tests__/init.js +++ b/lib/__tests__/init.js @@ -3,7 +3,7 @@ var initBook = require('../init'); describe('initBook', function() { - pit('should create a README and SUMMARY for empty book', function() { + it('should create a README and SUMMARY for empty book', function() { var dir = tmp.dirSync(); return initBook(dir.name) diff --git a/lib/fs/__tests__/mock.js b/lib/fs/__tests__/mock.js index 784201179..04bd46a2b 100644 --- a/lib/fs/__tests__/mock.js +++ b/lib/fs/__tests__/mock.js @@ -1,7 +1,6 @@ -jest.autoMockOff(); +var createMockFS = require('../mock'); describe('MockFS', function() { - var createMockFS = require('../mock'); var fs = createMockFS({ 'README.md': 'Hello World', 'SUMMARY.md': '# Summary', @@ -15,35 +14,35 @@ describe('MockFS', function() { }); describe('exists', function() { - pit('must return true for a file', function() { + it('must return true for a file', function() { return fs.exists('README.md') .then(function(result) { expect(result).toBeTruthy(); }); }); - pit('must return false for a non existing file', function() { + it('must return false for a non existing file', function() { return fs.exists('README_NOTEXISTS.md') .then(function(result) { expect(result).toBeFalsy(); }); }); - pit('must return true for a directory', function() { + it('must return true for a directory', function() { return fs.exists('folder') .then(function(result) { expect(result).toBeTruthy(); }); }); - pit('must return true for a deep file', function() { + it('must return true for a deep file', function() { return fs.exists('folder/test.md') .then(function(result) { expect(result).toBeTruthy(); }); }); - pit('must return true for a deep file (2)', function() { + it('must return true for a deep file (2)', function() { return fs.exists('folder/folder2/hello.md') .then(function(result) { expect(result).toBeTruthy(); @@ -52,14 +51,14 @@ describe('MockFS', function() { }); describe('readAsString', function() { - pit('must return content for a file', function() { + it('must return content for a file', function() { return fs.readAsString('README.md') .then(function(result) { expect(result).toBe('Hello World'); }); }); - pit('must return content for a deep file', function() { + it('must return content for a deep file', function() { return fs.readAsString('folder/test.md') .then(function(result) { expect(result).toBe('Cool'); @@ -68,7 +67,7 @@ describe('MockFS', function() { }); describe('readDir', function() { - pit('must return content for a directory', function() { + it('must return content for a directory', function() { return fs.readDir('./') .then(function(files) { expect(files.size).toBe(3); diff --git a/lib/models/__tests__/config.js b/lib/models/__tests__/config.js index 8445cef43..ff04ffde6 100644 --- a/lib/models/__tests__/config.js +++ b/lib/models/__tests__/config.js @@ -1,10 +1,7 @@ -jest.autoMockOff(); - var Immutable = require('immutable'); +var Config = require('../config'); describe('Config', function() { - var Config = require('../config'); - var config = Config.createWithValues({ hello: { world: 1, diff --git a/lib/models/__tests__/glossary.js b/lib/models/__tests__/glossary.js index 2ce224c23..5bf64dced 100644 --- a/lib/models/__tests__/glossary.js +++ b/lib/models/__tests__/glossary.js @@ -1,10 +1,8 @@ -jest.autoMockOff(); +var File = require('../file'); +var Glossary = require('../glossary'); +var GlossaryEntry = require('../glossaryEntry'); describe('Glossary', function() { - var File = require('../file'); - var Glossary = require('../glossary'); - var GlossaryEntry = require('../glossaryEntry'); - var glossary = Glossary.createFromEntries(File(), [ { name: 'Hello World', @@ -30,7 +28,7 @@ describe('Glossary', function() { }); describe('toText', function() { - pit('return as markdown', function() { + it('return as markdown', function() { return glossary.toText('.md') .then(function(text) { expect(text).toContain('# Glossary'); diff --git a/lib/models/__tests__/glossaryEntry.js b/lib/models/__tests__/glossaryEntry.js index 9eabc684c..833115d59 100644 --- a/lib/models/__tests__/glossaryEntry.js +++ b/lib/models/__tests__/glossaryEntry.js @@ -1,8 +1,6 @@ -jest.autoMockOff(); +var GlossaryEntry = require('../glossaryEntry'); describe('GlossaryEntry', function() { - var GlossaryEntry = require('../glossaryEntry'); - describe('getID', function() { it('must return a normalized ID', function() { var entry = new GlossaryEntry({ diff --git a/lib/models/__tests__/pluginDependency.js b/lib/models/__tests__/pluginDependency.js index 8aa55fbe3..0b105e9a4 100644 --- a/lib/models/__tests__/pluginDependency.js +++ b/lib/models/__tests__/pluginDependency.js @@ -1,6 +1,7 @@ -describe('PluginDependency', function() { - var PluginDependency = require('../pluginDependency'); +var Immutable = require('immutable'); +var PluginDependency = require('../pluginDependency'); +describe('PluginDependency', function() { describe('createFromString', function() { it('must parse name', function() { var plugin = PluginDependency.createFromString('hello'); @@ -42,6 +43,20 @@ describe('PluginDependency', function() { }); }); }); + + describe('listToArray', function() { + var list = PluginDependency.listToArray(Immutable.List([ + PluginDependency.createFromString('hello@1.0.0'), + PluginDependency.createFromString('noversion'), + PluginDependency.createFromString('-disabled') + ])); + + expect(list).toEqual([ + 'hello@1.0.0', + 'noversion', + '-disabled' + ]); + }); }); diff --git a/lib/models/__tests__/summary.js b/lib/models/__tests__/summary.js index f73e49baf..29c9330ac 100644 --- a/lib/models/__tests__/summary.js +++ b/lib/models/__tests__/summary.js @@ -82,7 +82,7 @@ describe('Summary', function() { }); describe('toText', function() { - pit('return as markdown', function() { + it('return as markdown', function() { return summary.toText('.md') .then(function(text) { expect(text).toContain('# Summary'); diff --git a/lib/models/__tests__/templateBlock.js b/lib/models/__tests__/templateBlock.js index fdd0210a2..e5f766619 100644 --- a/lib/models/__tests__/templateBlock.js +++ b/lib/models/__tests__/templateBlock.js @@ -6,7 +6,7 @@ describe('TemplateBlock', function() { var TemplateBlock = require('../templateBlock'); describe('create', function() { - pit('must initialize a simple TemplateBlock from a function', function() { + it('must initialize a simple TemplateBlock from a function', function() { var templateBlock = TemplateBlock.create('sayhello', function(block) { return { body: '

Hello, World!

', @@ -41,7 +41,7 @@ describe('TemplateBlock', function() { }; }); - expect(templateBlock.getShortcuts()).not.toBeDefined(); + expect(templateBlock.getShortcuts()).toNotExist(); }); it('must return complete shortcut', function() { @@ -67,7 +67,7 @@ describe('TemplateBlock', function() { }); describe('toNunjucksExt()', function() { - pit('should replace by block anchor', function() { + it('should replace by block anchor', function() { var templateBlock = TemplateBlock.create('sayhello', function(block) { return 'Hello'; }); @@ -97,7 +97,7 @@ describe('TemplateBlock', function() { }); }); - pit('must create a valid nunjucks extension', function() { + it('must create a valid nunjucks extension', function() { var templateBlock = TemplateBlock.create('sayhello', function(block) { return { body: '

Hello, World!

', @@ -120,7 +120,7 @@ describe('TemplateBlock', function() { }); }); - pit('must apply block arguments correctly', function() { + it('must apply block arguments correctly', function() { var templateBlock = TemplateBlock.create('sayhello', function(block) { return { body: '<'+block.kwargs.tag+'>Hello, '+block.kwargs.name+'!', @@ -143,7 +143,7 @@ describe('TemplateBlock', function() { }); }); - pit('must accept an async function', function() { + it('must accept an async function', function() { var templateBlock = TemplateBlock.create('sayhello', function(block) { return Promise() .then(function() { @@ -169,7 +169,7 @@ describe('TemplateBlock', function() { }); }); - pit('must handle nested blocks', function() { + it('must handle nested blocks', function() { var templateBlock = new TemplateBlock({ name: 'yoda', blocks: Immutable.List(['start', 'end']), diff --git a/lib/models/config.js b/lib/models/config.js index 3310a9333..070fe9269 100644 --- a/lib/models/config.js +++ b/lib/models/config.js @@ -68,6 +68,20 @@ Config.prototype.getPluginDependencies = function() { } }; +/** + Return a plugin dependency by its name + + @param {String} name + @return {PluginDependency} +*/ +Config.prototype.getPluginDependency = function(name) { + var plugins = this.getPluginDependencies(); + + return plugins.find(function(dep) { + return dep.getName() === name; + }); +}; + /** Update the list of plugins dependencies @@ -75,7 +89,7 @@ Config.prototype.getPluginDependencies = function() { @return {Config} */ Config.prototype.setPluginDependencies = function(deps) { - var plugins = PluginDependency.listFromArray(deps); + var plugins = PluginDependency.listToArray(deps); return this.setValue('plugins', plugins); }; diff --git a/lib/models/pluginDependency.js b/lib/models/pluginDependency.js index bb8e4b4b1..3502468da 100644 --- a/lib/models/pluginDependency.js +++ b/lib/models/pluginDependency.js @@ -29,6 +29,24 @@ PluginDependency.prototype.isEnabled = function() { return this.get('enabled'); }; +/** + Create a plugin with a name and a plugin + + @param {String} + @return {Plugin|undefined} +*/ +PluginDependency.create = function(name, version, enabled) { + if (is.undefined(enabled)) { + enabled = true; + } + + return new PluginDependency({ + name: name, + version: version || DEFAULT_VERSION, + enabled: Boolean(enabled) + }); +}; + /** Create a plugin from a string @@ -90,15 +108,15 @@ PluginDependency.listFromArray = function(arr) { /** Export plugin dependencies as an array - @param {List} + @param {List} list @return {Array} */ -PluginDependency.listToArray = function(arr) { - return arr +PluginDependency.listToArray = function(list) { + return list .map(function(dep) { - var result; + var result = ''; - if (dep.isEnabled()) { + if (!dep.isEnabled()) { result += '-'; } diff --git a/lib/modifiers/config/__tests__/addPlugin.js b/lib/modifiers/config/__tests__/addPlugin.js new file mode 100644 index 000000000..319a98028 --- /dev/null +++ b/lib/modifiers/config/__tests__/addPlugin.js @@ -0,0 +1,35 @@ +var addPlugin = require('../addPlugin'); +var Config = require('../../../models/config'); +var Book = require('../../../models/book'); + +describe('addPlugin', function() { + var config = Config.createWithValues({ + plugins: ['hello', 'world', '-disabled'] + }); + var book = Book().setConfig(config); + + it('should have correct state of dependencies', function() { + var disabledDep = config.getPluginDependency('disabled'); + + expect(disabledDep).toBeDefined(); + expect(disabledDep.getVersion()).toEqual('*'); + expect(disabledDep.isEnabled()).toBeFalsy(); + }); + + it('should add the plugin to the list', function() { + var newBook = addPlugin(book, 'test'); + var newConfig = newBook.getConfig(); + + var testDep = newConfig.getPluginDependency('test'); + expect(testDep).toBeDefined(); + expect(testDep.getVersion()).toEqual('*'); + expect(testDep.isEnabled()).toBeTruthy(); + + var disabledDep = newConfig.getPluginDependency('disabled'); + expect(disabledDep).toBeDefined(); + expect(disabledDep.getVersion()).toEqual('*'); + expect(disabledDep.isEnabled()).toBeFalsy(); + }); +}); + + diff --git a/lib/modifiers/config/addPlugin.js b/lib/modifiers/config/addPlugin.js index 2a04a031b..b4fd2272a 100644 --- a/lib/modifiers/config/addPlugin.js +++ b/lib/modifiers/config/addPlugin.js @@ -11,10 +11,7 @@ function addPlugin(book, plugin, version) { var config = book.getConfig(); var deps = config.getPluginDependencies(); - var dep = PluginDependency({ - name: plugin, - version: version - }); + var dep = PluginDependency.create(plugin, version); deps = deps.push(dep); config = config.setPluginDependencies(deps); diff --git a/lib/output/__tests__/ebook.js b/lib/output/__tests__/ebook.js index dabf360af..bcac9904b 100644 --- a/lib/output/__tests__/ebook.js +++ b/lib/output/__tests__/ebook.js @@ -3,7 +3,7 @@ var EbookGenerator = require('../ebook'); describe('EbookGenerator', function() { - pit('should generate a SUMMARY.html', function() { + it('should generate a SUMMARY.html', function() { return generateMock(EbookGenerator, { 'README.md': 'Hello World' }) diff --git a/lib/output/__tests__/json.js b/lib/output/__tests__/json.js index 94a036234..46ab42fc5 100644 --- a/lib/output/__tests__/json.js +++ b/lib/output/__tests__/json.js @@ -3,7 +3,7 @@ var JSONGenerator = require('../json'); describe('JSONGenerator', function() { - pit('should generate a README.json', function() { + it('should generate a README.json', function() { return generateMock(JSONGenerator, { 'README.md': 'Hello World' }) @@ -12,7 +12,7 @@ describe('JSONGenerator', function() { }); }); - pit('should generate a json file for each articles', function() { + it('should generate a json file for each articles', function() { return generateMock(JSONGenerator, { 'README.md': 'Hello World', 'SUMMARY.md': '# Summary\n\n* [Page](test/page.md)', diff --git a/lib/output/__tests__/website.js b/lib/output/__tests__/website.js index d9fe12328..554c56cd3 100644 --- a/lib/output/__tests__/website.js +++ b/lib/output/__tests__/website.js @@ -3,7 +3,7 @@ var WebsiteGenerator = require('../website'); describe('WebsiteGenerator', function() { - pit('should generate an index.html', function() { + it('should generate an index.html', function() { return generateMock(WebsiteGenerator, { 'README.md': 'Hello World' }) @@ -12,7 +12,7 @@ describe('WebsiteGenerator', function() { }); }); - pit('should copy asset files', function() { + it('should copy asset files', function() { return generateMock(WebsiteGenerator, { 'README.md': 'Hello World', 'myJsFile.js': 'var a = "test";', @@ -27,7 +27,7 @@ describe('WebsiteGenerator', function() { }); }); - pit('should generate an index.html for AsciiDoc', function() { + it('should generate an index.html for AsciiDoc', function() { return generateMock(WebsiteGenerator, { 'README.adoc': 'Hello World' }) @@ -36,7 +36,7 @@ describe('WebsiteGenerator', function() { }); }); - pit('should generate an HTML file for each articles', function() { + it('should generate an HTML file for each articles', function() { return generateMock(WebsiteGenerator, { 'README.md': 'Hello World', 'SUMMARY.md': '# Summary\n\n* [Page](test/page.md)', @@ -50,7 +50,7 @@ describe('WebsiteGenerator', function() { }); }); - pit('should not generate file if entry file doesn\'t exist', function() { + it('should not generate file if entry file doesn\'t exist', function() { return generateMock(WebsiteGenerator, { 'README.md': 'Hello World', 'SUMMARY.md': '# Summary\n\n* [Page 1](page.md)\n* [Page 2](test/page.md)', @@ -60,12 +60,12 @@ describe('WebsiteGenerator', function() { }) .then(function(folder) { expect(folder).toHaveFile('index.html'); - expect(folder).not.toHaveFile('page.html'); + expect(folder).toNotHaveFile('page.html'); expect(folder).toHaveFile('test/page.html'); }); }); - pit('should generate a multilingual book', function() { + it('should generate a multilingual book', function() { return generateMock(WebsiteGenerator, { 'LANGS.md': '# Languages\n\n* [en](en)\n* [fr](fr)', 'en': { @@ -81,12 +81,12 @@ describe('WebsiteGenerator', function() { expect(folder).toHaveFile('fr/index.html'); // Should not copy languages as assets - expect(folder).not.toHaveFile('en/README.md'); - expect(folder).not.toHaveFile('fr/README.md'); + expect(folder).toNotHaveFile('en/README.md'); + expect(folder).toNotHaveFile('fr/README.md'); // Should copy assets only once expect(folder).toHaveFile('gitbook/style.css'); - expect(folder).not.toHaveFile('en/gitbook/style.css'); + expect(folder).toNotHaveFile('en/gitbook/style.css'); expect(folder).toHaveFile('index.html'); }); diff --git a/lib/output/modifiers/__tests__/addHeadingId.js b/lib/output/modifiers/__tests__/addHeadingId.js index 7277440ea..a3b1d81f7 100644 --- a/lib/output/modifiers/__tests__/addHeadingId.js +++ b/lib/output/modifiers/__tests__/addHeadingId.js @@ -1,11 +1,8 @@ -jest.autoMockOff(); - var cheerio = require('cheerio'); +var addHeadingId = require('../addHeadingId'); describe('addHeadingId', function() { - var addHeadingId = require('../addHeadingId'); - - pit('should add an ID if none', function() { + it('should add an ID if none', function() { var $ = cheerio.load('

Hello World

Cool !!

'); return addHeadingId($) @@ -15,7 +12,7 @@ describe('addHeadingId', function() { }); }); - pit('should not change existing IDs', function() { + it('should not change existing IDs', function() { var $ = cheerio.load('

Hello World

'); return addHeadingId($) diff --git a/lib/output/modifiers/__tests__/annotateText.js b/lib/output/modifiers/__tests__/annotateText.js index 15d4c30e3..40b1e6c30 100644 --- a/lib/output/modifiers/__tests__/annotateText.js +++ b/lib/output/modifiers/__tests__/annotateText.js @@ -1,12 +1,9 @@ -jest.autoMockOff(); - var Immutable = require('immutable'); var cheerio = require('cheerio'); var GlossaryEntry = require('../../../models/glossaryEntry'); +var annotateText = require('../annotateText'); describe('annotateText', function() { - var annotateText = require('../annotateText'); - var entries = Immutable.List([ GlossaryEntry({ name: 'Word' }), GlossaryEntry({ name: 'Multiple Words' }) diff --git a/lib/output/modifiers/__tests__/fetchRemoteImages.js b/lib/output/modifiers/__tests__/fetchRemoteImages.js index f5610a23f..bc1704d01 100644 --- a/lib/output/modifiers/__tests__/fetchRemoteImages.js +++ b/lib/output/modifiers/__tests__/fetchRemoteImages.js @@ -12,7 +12,7 @@ describe('fetchRemoteImages', function() { dir = tmp.dirSync(); }); - pit('should download image file', function() { + it('should download image file', function() { var $ = cheerio.load(''); return fetchRemoteImages(dir.name, 'index.html', $) @@ -24,7 +24,7 @@ describe('fetchRemoteImages', function() { }); }); - pit('should download image file and replace with relative path', function() { + it('should download image file and replace with relative path', function() { var $ = cheerio.load(''); return fetchRemoteImages(dir.name, 'test/index.html', $) diff --git a/lib/output/modifiers/__tests__/highlightCode.js b/lib/output/modifiers/__tests__/highlightCode.js index bd7d42202..75d99022e 100644 --- a/lib/output/modifiers/__tests__/highlightCode.js +++ b/lib/output/modifiers/__tests__/highlightCode.js @@ -1,11 +1,8 @@ -jest.autoMockOff(); - var cheerio = require('cheerio'); var Promise = require('../../../utils/promise'); +var highlightCode = require('../highlightCode'); describe('highlightCode', function() { - var highlightCode = require('../highlightCode'); - function doHighlight(lang, code) { return { text: '' + (lang || '') + '$' + code @@ -19,7 +16,7 @@ describe('highlightCode', function() { }); } - pit('should call it for normal code element', function() { + it('should call it for normal code element', function() { var $ = cheerio.load('

This is a test

'); return highlightCode(doHighlight, $) @@ -29,7 +26,7 @@ describe('highlightCode', function() { }); }); - pit('should call it for markdown code block', function() { + it('should call it for markdown code block', function() { var $ = cheerio.load('
test
'); return highlightCode(doHighlight, $) @@ -39,7 +36,7 @@ describe('highlightCode', function() { }); }); - pit('should call it for asciidoc code block', function() { + it('should call it for asciidoc code block', function() { var $ = cheerio.load('
test
'); return highlightCode(doHighlight, $) @@ -49,7 +46,7 @@ describe('highlightCode', function() { }); }); - pit('should accept async highlighter', function() { + it('should accept async highlighter', function() { var $ = cheerio.load('
test
'); return highlightCode(doHighlightAsync, $) diff --git a/lib/output/modifiers/__tests__/inlinePng.js b/lib/output/modifiers/__tests__/inlinePng.js index fb094f79e..0073cffa7 100644 --- a/lib/output/modifiers/__tests__/inlinePng.js +++ b/lib/output/modifiers/__tests__/inlinePng.js @@ -9,7 +9,7 @@ describe('inlinePng', function() { dir = tmp.dirSync(); }); - pit('should write an inline PNG using data URI as a file', function() { + it('should write an inline PNG using data URI as a file', function() { var $ = cheerio.load('GitBook Logo 20x20'); return inlinePng(dir.name, 'index.html', $) diff --git a/lib/output/modifiers/__tests__/resolveLinks.js b/lib/output/modifiers/__tests__/resolveLinks.js index 3d50d80ac..9b7621b8a 100644 --- a/lib/output/modifiers/__tests__/resolveLinks.js +++ b/lib/output/modifiers/__tests__/resolveLinks.js @@ -1,11 +1,8 @@ -jest.autoMockOff(); - var path = require('path'); var cheerio = require('cheerio'); +var resolveLinks = require('../resolveLinks'); describe('resolveLinks', function() { - var resolveLinks = require('../resolveLinks'); - function resolveFileBasic(href) { return href; } @@ -21,7 +18,7 @@ describe('resolveLinks', function() { describe('Absolute path', function() { var TEST = '

This is a

'; - pit('should resolve path starting by "/" in root directory', function() { + it('should resolve path starting by "/" in root directory', function() { var $ = cheerio.load(TEST); return resolveLinks('hello.md', resolveFileBasic, $) @@ -31,7 +28,7 @@ describe('resolveLinks', function() { }); }); - pit('should resolve path starting by "/" in child directory', function() { + it('should resolve path starting by "/" in child directory', function() { var $ = cheerio.load(TEST); return resolveLinks('afolder/hello.md', resolveFileBasic, $) @@ -45,7 +42,7 @@ describe('resolveLinks', function() { describe('Custom Resolver', function() { var TEST = '

This is a

'; - pit('should resolve path correctly for absolute path', function() { + it('should resolve path correctly for absolute path', function() { var $ = cheerio.load(TEST); return resolveLinks('hello.md', resolveFileCustom, $) @@ -55,7 +52,7 @@ describe('resolveLinks', function() { }); }); - pit('should resolve path correctly for absolute path (2)', function() { + it('should resolve path correctly for absolute path (2)', function() { var $ = cheerio.load(TEST); return resolveLinks('afodler/hello.md', resolveFileCustom, $) diff --git a/lib/output/modifiers/__tests__/svgToImg.js b/lib/output/modifiers/__tests__/svgToImg.js index 793395e45..5fe979614 100644 --- a/lib/output/modifiers/__tests__/svgToImg.js +++ b/lib/output/modifiers/__tests__/svgToImg.js @@ -9,7 +9,7 @@ describe('svgToImg', function() { dir = tmp.dirSync(); }); - pit('should write svg as a file', function() { + it('should write svg as a file', function() { var $ = cheerio.load(''); return svgToImg(dir.name, 'index.html', $) diff --git a/lib/output/modifiers/__tests__/svgToPng.js b/lib/output/modifiers/__tests__/svgToPng.js index 163d72ec4..dbb350296 100644 --- a/lib/output/modifiers/__tests__/svgToPng.js +++ b/lib/output/modifiers/__tests__/svgToPng.js @@ -2,16 +2,17 @@ var cheerio = require('cheerio'); var tmp = require('tmp'); var path = require('path'); +var svgToImg = require('../svgToImg'); +var svgToPng = require('../svgToPng'); + describe('svgToPng', function() { var dir; - var svgToImg = require('../svgToImg'); - var svgToPng = require('../svgToPng'); beforeEach(function() { dir = tmp.dirSync(); }); - pit('should write svg as png file', function() { + it('should write svg as png file', function() { var $ = cheerio.load(''); var fileName = 'index.html'; diff --git a/lib/parse/__tests__/listAssets.js b/lib/parse/__tests__/listAssets.js index 2f66fd15d..4c5b0a0e2 100644 --- a/lib/parse/__tests__/listAssets.js +++ b/lib/parse/__tests__/listAssets.js @@ -6,7 +6,7 @@ var listAssets = require('../listAssets'); var parseGlossary = require('../parseGlossary'); describe('listAssets', function() { - pit('should not list glossary as asset', function() { + it('should not list glossary as asset', function() { var fs = createMockFS({ 'GLOSSARY.md': '# Glossary\n\n## Hello\nDescription for hello', 'assetFile.js': '', diff --git a/lib/parse/__tests__/parseBook.js b/lib/parse/__tests__/parseBook.js index 25d1802df..912d32d37 100644 --- a/lib/parse/__tests__/parseBook.js +++ b/lib/parse/__tests__/parseBook.js @@ -4,7 +4,7 @@ var createMockFS = require('../../fs/mock'); describe('parseBook', function() { var parseBook = require('../parseBook'); - pit('should parse multilingual book', function() { + it('should parse multilingual book', function() { var fs = createMockFS({ 'LANGS.md': '# Languages\n\n* [en](en)\n* [fr](fr)', 'en': { @@ -27,7 +27,7 @@ describe('parseBook', function() { }); }); - pit('should parse book in a directory', function() { + it('should parse book in a directory', function() { var fs = createMockFS({ 'book.json': JSON.stringify({ root: './test' diff --git a/lib/parse/__tests__/parseGlossary.js b/lib/parse/__tests__/parseGlossary.js index 53805fecd..9069af653 100644 --- a/lib/parse/__tests__/parseGlossary.js +++ b/lib/parse/__tests__/parseGlossary.js @@ -4,7 +4,7 @@ var createMockFS = require('../../fs/mock'); describe('parseGlossary', function() { var parseGlossary = require('../parseGlossary'); - pit('should parse glossary if exists', function() { + it('should parse glossary if exists', function() { var fs = createMockFS({ 'GLOSSARY.md': '# Glossary\n\n## Hello\nDescription for hello' }); @@ -21,7 +21,7 @@ describe('parseGlossary', function() { }); }); - pit('should not fail if doesn\'t exist', function() { + it('should not fail if doesn\'t exist', function() { var fs = createMockFS({}); var book = Book.createForFS(fs); diff --git a/lib/parse/__tests__/parseIgnore.js b/lib/parse/__tests__/parseIgnore.js index bee4236a6..54e7daeb0 100644 --- a/lib/parse/__tests__/parseIgnore.js +++ b/lib/parse/__tests__/parseIgnore.js @@ -17,21 +17,21 @@ describe('parseIgnore', function() { return parseIgnore(book); } - pit('should load rules from .ignore', function() { + it('should load rules from .ignore', function() { return getBook() .then(function(book) { expect(book.isFileIgnored('test-1.js')).toBeTruthy(); }); }); - pit('should load rules from .gitignore', function() { + it('should load rules from .gitignore', function() { return getBook() .then(function(book) { expect(book.isFileIgnored('test-2.js')).toBeTruthy(); }); }); - pit('should load rules from .bookignore', function() { + it('should load rules from .bookignore', function() { return getBook() .then(function(book) { expect(book.isFileIgnored('test-3.js')).toBeFalsy(); diff --git a/lib/parse/__tests__/parseReadme.js b/lib/parse/__tests__/parseReadme.js index 1b1567ba7..4270ea3e8 100644 --- a/lib/parse/__tests__/parseReadme.js +++ b/lib/parse/__tests__/parseReadme.js @@ -5,7 +5,7 @@ var createMockFS = require('../../fs/mock'); describe('parseReadme', function() { var parseReadme = require('../parseReadme'); - pit('should parse summary if exists', function() { + it('should parse summary if exists', function() { var fs = createMockFS({ 'README.md': '# Hello\n\nAnd here is the description.' }); @@ -22,7 +22,7 @@ describe('parseReadme', function() { }); }); - pit('should fail if doesn\'t exist', function() { + it('should fail if doesn\'t exist', function() { var fs = createMockFS({}); var book = Book.createForFS(fs); diff --git a/lib/parse/__tests__/parseSummary.js b/lib/parse/__tests__/parseSummary.js index 4b4650d87..55a445e3d 100644 --- a/lib/parse/__tests__/parseSummary.js +++ b/lib/parse/__tests__/parseSummary.js @@ -4,7 +4,7 @@ var createMockFS = require('../../fs/mock'); describe('parseSummary', function() { var parseSummary = require('../parseSummary'); - pit('should parse summary if exists', function() { + it('should parse summary if exists', function() { var fs = createMockFS({ 'SUMMARY.md': '# Summary\n\n* [Hello](hello.md)' }); @@ -19,7 +19,7 @@ describe('parseSummary', function() { }); }); - pit('should not fail if doesn\'t exist', function() { + it('should not fail if doesn\'t exist', function() { var fs = createMockFS({}); var book = Book.createForFS(fs); diff --git a/lib/plugins/__tests__/findInstalled.js b/lib/plugins/__tests__/findInstalled.js index 85e133f21..9377190e6 100644 --- a/lib/plugins/__tests__/findInstalled.js +++ b/lib/plugins/__tests__/findInstalled.js @@ -4,7 +4,7 @@ var Immutable = require('immutable'); describe('findInstalled', function() { var findInstalled = require('../findInstalled'); - pit('must list default plugins for gitbook directory', function() { + it('must list default plugins for gitbook directory', function() { // Read gitbook-plugins from package.json var pkg = require(path.resolve(__dirname, '../../../package.json')); var gitbookPlugins = Immutable.Seq(pkg.dependencies) diff --git a/lib/plugins/__tests__/validatePlugin.js b/lib/plugins/__tests__/validatePlugin.js index 3d508399b..635423cc6 100644 --- a/lib/plugins/__tests__/validatePlugin.js +++ b/lib/plugins/__tests__/validatePlugin.js @@ -1,13 +1,9 @@ -jest.autoMockOff(); - var Promise = require('../../utils/promise'); var Plugin = require('../../models/plugin'); - +var validatePlugin = require('../validatePlugin'); describe('validatePlugin', function() { - var validatePlugin = require('../validatePlugin'); - - pit('must not validate a not loaded plugin', function() { + it('must not validate a not loaded plugin', function() { var plugin = Plugin.createFromString('test'); return validatePlugin(plugin) @@ -17,5 +13,4 @@ describe('validatePlugin', function() { return Promise(); }); }); - }); diff --git a/lib/templating/__tests__/conrefsLoader.js b/lib/templating/__tests__/conrefsLoader.js index ff484b70c..d430a4f67 100644 --- a/lib/templating/__tests__/conrefsLoader.js +++ b/lib/templating/__tests__/conrefsLoader.js @@ -9,21 +9,21 @@ describe('ConrefsLoader', function() { }); describe('Git', function() { - pit('should include content from git', function() { + it('should include content from git', function() { return renderTemplate(engine, 'test.md', '{% include "git+https://gist.github.com/69ea4542e4c8967d2fa7.git/test.md" %}') .then(function(out) { expect(out.getContent()).toBe('Hello from git'); }); }); - pit('should handle deep inclusion (1)', function() { + it('should handle deep inclusion (1)', function() { return renderTemplate(engine, 'test.md', '{% include "git+https://gist.github.com/69ea4542e4c8967d2fa7.git/test2.md" %}') .then(function(out) { expect(out.getContent()).toBe('First Hello. Hello from git'); }); }); - pit('should handle deep inclusion (2)', function() { + it('should handle deep inclusion (2)', function() { return renderTemplate(engine, 'test.md', '{% include "git+https://gist.github.com/69ea4542e4c8967d2fa7.git/test3.md" %}') .then(function(out) { expect(out.getContent()).toBe('First Hello. Hello from git'); diff --git a/lib/utils/__tests__/git.js b/lib/utils/__tests__/git.js index 90555b6aa..abc1ea1cb 100644 --- a/lib/utils/__tests__/git.js +++ b/lib/utils/__tests__/git.js @@ -15,8 +15,8 @@ describe('Git', function() { expect(Git.isUrl('git+git@github.com:GitbookIO/gitbook.git/directory/README.md#e1594cde2c32e4ff48f6c4eff3d3d461743d74e1')).toBeTruthy(); // Non valid - expect(Git.isUrl('https://github.com/Hello/world.git')).not.toBeTruthy(); - expect(Git.isUrl('README.md')).not.toBeTruthy(); + expect(Git.isUrl('https://github.com/Hello/world.git')).toBeFalsy(); + expect(Git.isUrl('README.md')).toBeFalsy(); }); it('should parse HTTPS urls', function() { @@ -45,7 +45,7 @@ describe('Git', function() { }); describe('Cloning and resolving', function() { - pit('should clone an HTTPS url', function() { + it('should clone an HTTPS url', function() { var git = new Git(path.join(os.tmpdir(), 'test-git-'+Date.now())); return git.resolve('git+https://gist.github.com/69ea4542e4c8967d2fa7.git/test.md') .then(function(filename) { diff --git a/lib/utils/__tests__/location.js b/lib/utils/__tests__/location.js index 1d75751dc..2d017147f 100644 --- a/lib/utils/__tests__/location.js +++ b/lib/utils/__tests__/location.js @@ -1,8 +1,6 @@ -jest.autoMockOff(); +var LocationUtils = require('../location'); describe('LocationUtils', function() { - var LocationUtils = require('../location'); - it('should correctly test external location', function() { expect(LocationUtils.isExternal('http://google.fr')).toBe(true); expect(LocationUtils.isExternal('https://google.fr')).toBe(true); diff --git a/package.json b/package.json index 0658afedf..124558c6d 100644 --- a/package.json +++ b/package.json @@ -62,11 +62,12 @@ }, "devDependencies": { "eslint": "2.9.0", - "jest-cli": "12.0.2" + "expect": "^1.20.1", + "mocha": "^2.4.5" }, "scripts": { - "test": "node_modules/.bin/jest --bail", - "lint": "eslint ." + "lint": "eslint .", + "test": "./node_modules/.bin/mocha ./testing/setup.js './lib/**/*/__tests__/*.js' --bail --reporter=list --timeout=5000" }, "repository": { "type": "git", @@ -94,12 +95,5 @@ "name": "Samy Pessé", "email": "samy@gitbook.com" } - ], - "jest": { - "automock": false, - "setupTestFrameworkScriptFile": "/jest/customMatchers.js", - "globals": { - "__DEV__": true - } - } + ] } diff --git a/testing/setup.js b/testing/setup.js new file mode 100644 index 000000000..b91a2994a --- /dev/null +++ b/testing/setup.js @@ -0,0 +1,49 @@ +var is = require('is'); +var path = require('path'); +var fs = require('fs'); +var expect = require('expect'); + +expect.extend({ + /** + Check that a file is created in a directory: + + expect('myFolder').toHaveFile('hello.md'); + */ + toHaveFile: function(fileName) { + var filePath = path.join(this.actual, fileName); + var exists = fs.existsSync(filePath); + + expect.assert( + exists, + 'expected %s to have file %s', + this.actual, + fileName + ); + return this; + }, + toNotHaveFile: function(fileName) { + var filePath = path.join(this.actual, fileName); + var exists = fs.existsSync(filePath); + + expect.assert( + !exists, + 'expected %s to not have file %s', + this.actual, + fileName + ); + return this; + }, + + /** + Check that a value is defined (not null nor undefined) + */ + toBeDefined: function() { + expect.assert( + !(is.undefined(this.actual) || is.null(this.actual)), + 'expected to be defined' + ); + return this; + } +}); + +global.expect = expect;