tj / co-views

Higher-level template rendering for node.js using generators
173 stars 24 forks source link

Should template functions be cached when NODE_ENV is "test"? #8

Closed ryankask closed 9 years ago

ryankask commented 10 years ago

This threw me off for a bit when using supertest and koa. When the cache is on I get something like:

2 >> dispatch status: undefined Not Found header: x-powered-by: koa body: undefined

It would be good to understand why this happens too.

tj commented 10 years ago

do you have a reproducible script I can take a look at?

ryankask commented 10 years ago

Hopefully the below is useful (notice the change in NODE_ENV):

$ DEBUG=koa* NODE_ENV=development node_modules/.bin/mocha --harmony co-view-test
.js
  koa-route GET / -> /^\/\/?$/i +0ms
  koa:application use - +0ms

    koa:application listen +3ms
Warning: do not run DEBUG=koa-compose in production
as it will greatly affect the performance of your
application - it is designed for a development
environment only.

  0 >>
  status: undefined Not Found
  header:
    x-powered-by: koa
  body: undefined

  koa-route GET / matches / [] +17ms
  0 <<
  status: 200 OK
  header:
    x-powered-by: koa
    content-type: text/html; charset=utf-8
    content-length: 47
  body: "<html>\n  <head></head>\n  <body></body>\n</html>\n"

․

  1 passing (43ms)

Failing:

$ DEBUG=koa* NODE_ENV=test node_modules/.bin/mocha --harmony co-view-test.js

koa-route GET / -> /^\/\/?$/i +0ms
  koa:application use - +0ms

    koa:application listen +3ms

  0 >>
  status: undefined Not Found
  header:
    x-powered-by: koa
  body: undefined

  koa-route GET / matches / [] +17ms
․

  0 passing (42ms)
  1 failing
var koa = require('koa');
var path = require('path');
var request = require('supertest');
var views = require('co-views');
var _ = require('koa-route');

var render = views(path.join(__dirname, 'views'), {
  map: { html: 'swig'}
});

var app = koa();

app.use(_.get('/', function *() {
  this.body = yield render('home');
}));

describe('routes', function() {
  it('it should return 200', function(done) {
    request(app.listen())
      .get('/')
      .expect(200, done);
  });
});
ryankask commented 10 years ago

The issue seems to be with the values of cache. It should either be false or 'memory' -- true is not valid.

From the swig docs:

cache   CacheOptions    Cache control for templates. Defaults to saving in 'memory'. Send false to     disable. Send an object with get and set functions to customize.
diorahman commented 10 years ago

@ryankask yes very true, if you left the options.cache to be null it will be set to true by co-views (https://github.com/visionmedia/co-views/blob/master/index.js#L48) when the NODE_ENV != 'development' which is not a valid value for swig (https://github.com/paularmstrong/swig/issues/430). The safety belt (when using co-views with swig template) will be set the options.cache of co-views' init, e.g.

var views = require ('co-views');
var dir = './views'
var env = process.env.NODE_ENV || 'development'
views(dir, {
  map: { html: 'swig' },
  cache : env != 'production' ? false : 'memory'
});