kimroen / ember-cli-coffeescript

Adds precompilation of CoffeeScript files and all the basic generation types to the ember generate command.
MIT License
72 stars 49 forks source link

Test that generated blueprints are valid CoffeeScript #132

Closed kimroen closed 7 years ago

kimroen commented 7 years ago

Here's an example of a test for generating and destroying ember g adapter foo:

it('adapter foo', function() {
  var args = ['adapter', 'foo'];

  return emberNew()
    .then(() => emberGenerateDestroy(args, (file) => {
      expect(file('app/adapters/foo.coffee'))
        .to.contain("stuff");
  }));
});

What I want to do is to add a step here after the generator has run (but before it has been destroyed) that compiles the specified file with CoffeeScript and checks that it worked.

This will make sure we keep producing valid CoffeeScript, and should allow us to catch all kinds of bugs.

We could make our own test helper that compiles the specified file and verifies that it doesn't blow up. Something like this (just thinking out loud, this won't work):

it('adapter foo', function() {
  var args = ['adapter', 'foo'];

  return emberNew()
    .then(() => emberGenerateDestroy(args, (file) => {
      adapterFile = file('app/adapters/foo.coffee');
      expect(adapterFile)
        .to.contain("stuff");

      expect(compileCoffee(adapterFile)).to.not.throw(Error);
      // or something more contained like
      compileAndExpectCoffee(adapterFile);
  }));
});
jakesjews commented 7 years ago

@kimroen I made a small spike for this and wanted to run it past you to see if you like the direction. I made a test helper file which looks like

var coffeescript = require('coffee-script');
var expect = require('ember-cli-blueprint-test-helpers/chai').expect;

module.exports = function(file) {
  var compileFunc = function() {
    coffeescript.compile(file.content);
  }

  expect(compileFunc).to.not.throw(Error);
}

and a test file using it looks like

'use strict';

var blueprintHelpers = require('ember-cli-blueprint-test-helpers/helpers');
var setupTestHooks = blueprintHelpers.setupTestHooks;
var emberNew = blueprintHelpers.emberNew;
var emberGenerateDestroy = blueprintHelpers.emberGenerateDestroy;

var expect = require('ember-cli-blueprint-test-helpers/chai').expect;
var expectCoffee = require('../helpers/expect-coffee');

describe('Acceptance: ember generate and destroy acceptance-test', function() {
  setupTestHooks(this);

  it('acceptance-test foo', function() {
    var args = ['acceptance-test', 'foo'];

    return emberNew()
      .then(() => emberGenerateDestroy(args, (file) => {
        var acceptanceFile = file('tests/acceptance/foo-test.coffee');

        expect(acceptanceFile)
          .to.contain("import Ember from 'ember")
          .to.contain("import { module, test } from 'qunit'")
          .to.contain("import startApp from 'my-app/tests/helpers/start-app'")
          .to.contain("module 'Acceptance: Foo',")
          .to.contain("test 'visiting /foo', (assert) ->")
          .to.contain("visit '/foo'")
          .to.contain("andThen ->")
          .to.contain("assert.equal currentURL(), '/foo'");

        expectCoffee(acceptanceFile);
    }));
  });
});
kimroen commented 7 years ago

Yeah, that looks like a very good starting point 👍

This project doesn't depend on coffee-script directly though - will it work to require it like that anyway? Should we try to compile it using broccoli-coffee instead, maybe?

jakesjews commented 7 years ago

I added it to dev dependencies although maybe it would be better to use the implicit dependency from broccoli-coffee

kimroen commented 7 years ago

Fixed by #124 🎉