jamesarosen / ember-i18n

Other
764 stars 184 forks source link

Issue using ember-i18n in qunit 4.2.1 and ember-cli version 2.18.2 #483

Closed epchristi closed 6 years ago

epchristi commented 6 years ago

I am possibly doing something wrong in configuring this, but I have tried to follow the documents for setting up i18n for tests. In my qunit file, I have:

import { t } from 'ember-i18n/test-support';

In my test I am using t('common.testText')... assert.equal(page.title, t('common.testText');

But.. when the test executes, function t() fails, because the "getContext" call returns undefined, thus owner is undefined. Is that an expected case that should be handled? And/Or is this a setup issue?

jamesarosen commented 6 years ago

Have you read https://github.com/jamesarosen/ember-i18n/wiki/Doc:-Testing ?

epchristi commented 6 years ago

Yes, that's what I am trying to follow.

On Thu, Apr 12, 2018 at 12:49 PM, James Alexander Rosen < notifications@github.com> wrote:

Have you read https://github.com/jamesarosen/ember-i18n/wiki/Doc:-Testing ?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jamesarosen/ember-i18n/issues/483#issuecomment-380907536, or mute the thread https://github.com/notifications/unsubscribe-auth/APYKyVZnE1jXapI8QGh7a0jYd8iBiQO_ks5tn6G7gaJpZM4TSR4J .

epchristi commented 6 years ago

Yes. That is what I mentioned when I showed the import --- are there other steps missing? (I notice t is defined)

jamesarosen commented 6 years ago

Is that an expected case that should be handled?

Not expected.

And/Or is this a setup issue?

Possibly. It's also possible that ember-i18n's guides for using RFC232/RFC268 tests are wrong.

Perhaps you could share more of your test file.

epchristi commented 6 years ago

On thing that is different (although I tried using module), is we are using moduleForAcceptance to setup the qunit tests. The test itself is pretty simple.

in moduleForAcceptance('Acceptance | dashboard', { beforeEach() { // create a mock server for mirage // create a page } });

test('Networks title', async function(assert) { await(page.visit(...page setup in the beforeEach...)); assert.equal(page.title, t('common.networksText')); });

ember 2.18 still generates "qunit stubs" that use moduleForAcceptance. I wonder if that's part of the issue.

In my start-app.js helper I do: let application = Application.create(attributes); application.setupForTesting(); // tried setupApplicationTest, but that didn't work application.injectTestHelpers(); return application;

Then in our module-for-acceptance.js helper, it's pretty boiler-plate. this.application= startApp();

jamesarosen commented 6 years ago

You're using an "old-style" acceptance test, not an RFC268-style test. That means you should follow this section. Specifically, you should have

// tests/helpers/start-app.js
import './ember-i18n/test-helpers';

and shouldn't have import { t } from 'ember-i18n/test-support' in your test file.

epchristi commented 6 years ago

I am okay with you closing this, however, I still get the "assert" that owner isn't defined. I am going to back down to qunit 4.1.1 to see if using the "old" way works as you suggestion. Thank you.

jamesarosen commented 6 years ago

I didn't expect that! Do please document your findings here. We can add them to the wiki once you figure out what's going on.

epchristi commented 6 years ago

I should note, this all worked prior to upgrading to ember 2.18.2

I am now trying with Qunit 4.1.1 -- I can change this out if I should go to 4.2.1

In my start-app.js file, I added:
import './ember-i18n/test-helpers';

Here is what I have so far. In my "test", I am setting it up like this:

import { tHelper } from 'ember-i18n/helper';
import { getOwner } from '@ember/application';

moduleForAcceptance('Acceptance | dashboard', {
    beforeEach() {
        getOwner(this).lookup('service:i18n').set('locale', 'en');
        this.register('helper:t', tHelper);
        ...

First, eslint fails because of t being undefined. If this works, I can put a mirage ignore for those errors, not a huge deal. Second.. the getOwner fails. This is because getOwner(this) returns undefined.

so.. I tried removing that call. then, this.register is undefined.

So.. those steps are not working as expected.

jamesarosen commented 6 years ago

First, eslint fails because of t being undefined.

You can add t to tests/.eslintrc.js. It's a global test helper like visit or fillIn.

epchristi commented 6 years ago

okay. I backed qunit down to version 4.1.1. I removed all the register stuff and simply added the import to start-apps. I added t: true to globals of .eslintrc.js and now I have it working.

Thank you for your help! (I would close this, but realize there may be an issue with 4.2.1 using the old way.)

jamesarosen commented 6 years ago

jamesarosen/ember-i18n has been deprecated in favor of ember-intl.