Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test async and teardown #77

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
},
"devDependencies": {
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"http-server": "^0.11.1",
"mocha": "^6.1.4",
"nyc": "^14.0.0",
Expand Down
21 changes: 10 additions & 11 deletions test/cli/cases/build.config.default/build.config.default.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,23 @@
const runSmokeTest = require('../../smoke-test');
const TestBed = require('../../test-bed');

describe('Build Greenwood With: ', async () => {
describe('Build Greenwood With: ', async function() {
const LABEL = 'Empty Configuration and Default Workspace';
let setup;
let context;

before(async () => {
before(async function() {
setup = new TestBed();
context = setup.setupTestBed(__dirname);
this.context = setup.setupTestBed(__dirname);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confused about the switch to this though. What is this in this context anyway, in the middle of a describe block?

Maybe it's needed to fix this, if so, could you just expand? My only concern is just making sure all these tests remain as isolated as possible.

Copy link
Member Author

@hutchgrant hutchgrant May 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/mochajs/mocha/wiki/Shared-Behaviours

If you try to use the context within the describe and not part of this, while using context as a parameter for smokeTests and each test, you will receive the following

  6) Build Greenwood With: 
       Default Greenwood Configuration and Workspace
         Running Smoke Tests: Default Greenwood Configuration and Workspace
           404 (Not Found) page
             "before each" hook for "should have a <script> tag in the <body>":
     TypeError: Cannot read property 'publicDir' of undefined

The reason for that is, when the variable context is initialized in the before() via setup() it's not assigned to the describe's context at all. Because the context.publicDir, isn't working in the smokeTests([], context, label) after. That or we're not accessing the right context, from the child describe, when we're using it as a parameter.

Within each before, it, and after, you can share the same this variables(as long as you don't use arrow functions, test using one you'll see it breaks it).

Copy link
Member Author

@hutchgrant hutchgrant May 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it has something to do with all our nested describes and shared tests. In any event, yes it's required.

Using context instead of this.context, running

describe('Build Greenwood With: ', function() {
  const LABEL = 'Default Greenwood Configuration and Workspace';
  let setup;
  let context;

  before(function() {
    setup = new TestBed();
    context = setup.setupTestBed(__dirname);
  });
  
  describe(LABEL, function() {
    
    before(async function() {     
      await setup.runGreenwoodCommand('build');
    });
    runSmokeTest(['public', 'index', 'not-found', 'hello'], context, LABEL);
  });

  after(function() {
    setup.teardownTestBed();
  });
});

with smoke-test.js

// TOOD need to find a good Promise based way to call setup.teardownTestBed() after all tests are run
// right now, test files are left over after tests are run, but are cleaned up before each test run
module.exports = runSmokeTest = async function(testCases, context, label) {
  console.log(context);
}

will output undefined.

Also, if you're thinking of putting that runSmokeTest() within a nested test how it was previously, that will also not work because of the teardown. Granted, the context variable will output correctly, but everything else will fail because of teardown.

    it('run smoke test', function() {
      runSmokeTest(['public', 'index', 'not-found', 'hello'], context, LABEL);
    });

output:

  1) Running Smoke Tests: Default Greenwood Configuration and Workspace
       Public Directory Generated Output
         should create a public directory:

      AssertionError: expected false to be true
      + expected - actual

      -false
      +true
      
      at Context.<anonymous> (test/cli/smoke-test.js:11:55)

That error x7. Without teardown in after() yes, this runs correctly.

Trust me, it took a lot of fiddling to get shared tests working.

});

describe(LABEL, () => {
before(async () => {
describe(LABEL, function() {
before(async function() {
await setup.runGreenwoodCommand('build');
});

it('should pass all smoke tests', async () => {
await runSmokeTest(['public', 'index', 'not-found', 'hello'], context, setup, LABEL);
});
runSmokeTest(['public', 'index', 'not-found', 'hello'], LABEL);
});

after(function() {
setup.teardownTestBed();
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,9 @@ describe('Build Greenwood With: ', () => {
}
});
});

after(function() {
setup.teardownTestBed();
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ describe('Build Greenwood With: ', () => {
});
});

after(function() {
setup.teardownTestBed();
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,23 @@
const runSmokeTest = require('../../smoke-test');
const TestBed = require('../../test-bed');

describe('Build Greenwood With: ', () => {
describe('Build Greenwood With: ', function() {
const LABEL = 'Custom Configuration for Workspace (www) and Default Greenwood configuration';
let setup;
let context;

before(async () => {
before(async function() {
setup = new TestBed();
context = setup.setupTestBed(__dirname);
this.context = setup.setupTestBed(__dirname);
});

describe(LABEL, () => {
before(async () => {
describe(LABEL, function() {
before(async function() {
await setup.runGreenwoodCommand('build');
});

it('should pass all smoke tests', async () => {
await runSmokeTest(['public', 'index', 'not-found'], context, setup, LABEL);
});
runSmokeTest(['public', 'index', 'not-found'], LABEL);
});

after(function() {
setup.teardownTestBed();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,52 +25,53 @@ const path = require('path');
const TestBed = require('../../test-bed');

// TODO why does this case need a src/pages/index.md?
describe('Build Greenwood With: ', () => {
describe('Build Greenwood With: ', function() {
const LABEL = 'Default Greenwood Configuration and Default Workspace w/ Nested Directories';
let setup;
let context;

before(async () => {
before(async function() {
setup = new TestBed();
context = setup.setupTestBed(__dirname);
this.context = setup.setupTestBed(__dirname);
});

describe(LABEL, () => {
before(async () => {
describe(LABEL, function() {
before(async function() {
await setup.runGreenwoodCommand('build');
});

runSmokeTest(['public', 'not-found', 'index'], LABEL);

it('should pass smoke tests for public, not found, and index', async () => {
await runSmokeTest(['public', 'not-found', 'index'], context, setup, LABEL);
});

it('should create a default blog page directory', () => {
expect(fs.existsSync(path.join(context.publicDir, './blog'))).to.be.true;
it('should create a default blog page directory', function() {
expect(fs.existsSync(path.join(this.context.publicDir, './blog'))).to.be.true;
});

describe('Custom blog page directory', () => {
describe('Custom blog page directory', function() {
let dom;

beforeEach(async() => {
dom = await JSDOM.fromFile(path.resolve(context.publicDir, 'blog', '2019', './index.html'));
beforeEach(async function() {
dom = await JSDOM.fromFile(path.resolve(this.context.publicDir, 'blog', '2019', './index.html'));
});

it('should output an index.html file within the default hello page directory', () => {
expect(fs.existsSync(path.join(context.publicDir, 'blog', '2019', './index.html'))).to.be.true;
it('should output an index.html file within the default hello page directory', function() {
expect(fs.existsSync(path.join(this.context.publicDir, 'blog', '2019', './index.html'))).to.be.true;
});

it('should have the expected heading text within the hello example page in the hello directory', async() => {
it('should have the expected heading text within the hello example page in the hello directory', async function() {
const heading = dom.window.document.querySelector('h3').textContent;

expect(heading).to.equal('Blog Page');
});

it('should have the expected paragraph text within the hello example page in the hello directory', async() => {
it('should have the expected paragraph text within the hello example page in the hello directory', async function() {
let paragraph = dom.window.document.querySelector('p').textContent;

expect(paragraph).to.equal('This is the test blog page built by Greenwood.');
});
});
});

after(function() {
setup.teardownTestBed();
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -22,89 +22,89 @@ const { JSDOM } = require('jsdom');
const path = require('path');
const TestBed = require('../../test-bed');

describe('Build Greenwood With: ', () => {
describe('Build Greenwood With: ', function() {
const LABEL = 'Default Greenwood Configuration and Workspace w/Custom App Template';
let setup;
let context;

before(async () => {
before(async function() {
setup = new TestBed();
context = setup.setupTestBed(__dirname);
this.context = setup.setupTestBed(__dirname);
});

describe(LABEL, () => {
describe(LABEL, function() {
let dom;

before(async () => {
await setup.runGreenwoodCommand('build');
});

it('should pass all public, not-found, and hello smoke tests', async () => {
await runSmokeTest(['public', 'not-found', 'hello'], context, setup, LABEL);
});
runSmokeTest(['public', 'not-found', 'hello'], LABEL);

describe('Custom Index (Home) page', () => {
describe('Custom Index (Home) page', function() {
const indexPageHeading = 'Greenwood';
const indexPageBody = 'This is the home page built by Greenwood. Make your own pages in src/pages/index.js!';
let dom;

beforeEach(async() => {
dom = await JSDOM.fromFile(path.resolve(context.publicDir, 'index.html'));
beforeEach(async function() {
dom = await JSDOM.fromFile(path.resolve(this.context.publicDir, 'index.html'));
});

it('should have a <title> tag in the <head>', () => {
it('should have a <title> tag in the <head>', function() {
const title = dom.window.document.querySelector('head title').textContent;

expect(title).to.be.equal('My App');
});

it('should have a <script> tag in the <body>', () => {
it('should have a <script> tag in the <body>', function() {
const scriptTag = dom.window.document.querySelectorAll('body script');

expect(scriptTag.length).to.be.equal(1);
});

it('should have a router outlet tag in the <body>', () => {
it('should have a router outlet tag in the <body>', function() {
const outlet = dom.window.document.querySelectorAll('body eve-app');

expect(outlet.length).to.be.equal(1);
});

// no 404 route in our custom app-template.js, like greenwood does
it('should have the correct route tags in the <body>', () => {
it('should have the correct route tags in the <body>', function() {
const routes = dom.window.document.querySelectorAll('body lit-route');

expect(routes.length).to.be.equal(2);
});

it('should have the expected heading text within the index page in the public directory', () => {
it('should have the expected heading text within the index page in the public directory', function() {
const heading = dom.window.document.querySelector('h3').textContent;

expect(heading).to.equal(indexPageHeading);
});

it('should have the expected paragraph text within the index page in the public directory', () => {
it('should have the expected paragraph text within the index page in the public directory', function() {
let paragraph = dom.window.document.querySelector('p').textContent;

expect(paragraph).to.equal(indexPageBody);
});
});

describe('Custom App Template', () => {
before(async() => {
dom = await JSDOM.fromFile(path.resolve(context.publicDir, 'index.html'));
describe('Custom App Template', function() {
before(async function() {
dom = await JSDOM.fromFile(path.resolve(this.context.publicDir, 'index.html'));
});

it('should output a single index.html file using our custom app template', () => {
expect(fs.existsSync(path.join(context.publicDir, './index.html'))).to.be.true;
it('should output a single index.html file using our custom app template', function() {
expect(fs.existsSync(path.join(this.context.publicDir, './index.html'))).to.be.true;
});

it('should have the specific element we added as part of our custom app template', () => {
it('should have the specific element we added as part of our custom app template', function() {
const customParagraph = dom.window.document.querySelector('p#custom-app-template').textContent;

expect(customParagraph).to.equal('My Custom App Template');
});
});
});

after(function() {
setup.teardownTestBed();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,43 @@ const { JSDOM } = require('jsdom');
const path = require('path');
const TestBed = require('../../test-bed');

describe('Build Greenwood With: ', () => {
describe('Build Greenwood With: ', function() {
const LABEL = 'Default Greenwood Configuration and Workspace w/Custom Page Template';
let setup;
let context;

before(async () => {
before(async function() {
setup = new TestBed();
context = setup.setupTestBed(__dirname);
this.context = setup.setupTestBed(__dirname);
});

describe(LABEL, () => {
before(async() => {
describe(LABEL, function() {
before(async function() {
await setup.runGreenwoodCommand('build');
});

it('should pass all smoke tests', async () => {
await runSmokeTest(['public', 'index', 'not-found', 'hello'], context, setup, LABEL);
});
runSmokeTest(['public', 'index', 'not-found', 'hello'], LABEL);

describe('Custom Page Template', () => {
describe('Custom Page Template', function() {
let dom;

before(async() => {
dom = await JSDOM.fromFile(path.resolve(context.publicDir, 'index.html'));
before(async function() {
dom = await JSDOM.fromFile(path.resolve(this.context.publicDir, 'index.html'));
});

it('should output a single index.html file using our custom app template', () => {
expect(fs.existsSync(path.join(context.publicDir, './index.html'))).to.be.true;
it('should output a single index.html file using our custom app template', function() {
expect(fs.existsSync(path.join(this.context.publicDir, './index.html'))).to.be.true;
});

it('should have the specific element we added as part of our custom page template', () => {
it('should have the specific element we added as part of our custom page template', function() {
const customElement = dom.window.document.querySelectorAll('div.owen-test');

expect(customElement.length).to.equal(1);
});
});

});

after(function() {
setup.teardownTestBed();
});
});
20 changes: 10 additions & 10 deletions test/cli/cases/build.default/build.default.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@
const runSmokeTest = require('../../smoke-test');
const TestBed = require('../../test-bed');

describe('Build Greenwood With: ', () => {
describe('Build Greenwood With: ', function() {
const LABEL = 'Default Greenwood Configuration and Workspace';
let setup;
let context;

before(() => {
before(function() {
setup = new TestBed();
context = setup.setupTestBed(__dirname);
this.context = setup.setupTestBed(__dirname);
});

describe(LABEL, () => {
before(async () => {
await setup.runGreenwoodCommand('build');
});
describe(LABEL, function() {

it('should pass all smoke tests', async () => {
await runSmokeTest(['public', 'index', 'not-found', 'hello'], context, setup, LABEL);
before(async function() {
await setup.runGreenwoodCommand('build');
});
runSmokeTest(['public', 'index', 'not-found', 'hello'], LABEL);
});

after(function() {
setup.teardownTestBed();
});
});
Loading