我想测试我的AuthModule控制器HTTP层与supertest作为官方描述Nestjs文档。此模块使用EmailService从EmailModule.
我知道您可以按如下方式覆盖提供程序:
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [
AuthModule,
],
})
.overrideProvider(EmailService)
.useValue(buildMock(EmailService))
Run Code Online (Sandbox Code Playgroud)
但是,这并不工作,我假设,因为EmailModule在进口AuthModule。一种解决方法是.overrideProvider(...).useValue(...)在每一个供应商EmailModule,但这种非意义,因为我接下来要由进口也模拟模块EmailModule。
当我进行 e2e 测试时AuthModule,老实说,我并不关心它的EmailModule工作原理。我需要模拟的是EmailService并确保我的身份验证模块与该服务正确交互。
不幸的Test.createTestingModule({})是,没有.overrideModule()方法。
我试着EmailModule用开玩笑的方式嘲笑:
jest.mock('../../src/email/email.module.ts', () => {
@Module({})
class EmailModule {
}
return EmailModule;
});
Run Code Online (Sandbox Code Playgroud)
但我收到此错误:
Error: Nest cannot create the module instance. Often, this is because of …Run Code Online (Sandbox Code Playgroud) I'm well aware of what the Hook has missing dependency is, what it means and why it's important to watch all dependencies, but this one is just weird.
export function Compo() {
const [value, setValue] = useState<number>();
useEffect(() => {
setValue(Date.now());
}, []);
return (
<>{value}</>
);
}
Run Code Online (Sandbox Code Playgroud)
works fine, but:
function useValue() {
return useState<number>();
}
export function Compo() {
const [value, setValue] = useValue();
useEffect(() => {
setValue(Date.now());
}, []);
return (
<>{value}</>
);
}
Run Code Online (Sandbox Code Playgroud)
show the well …
使用 UglifyJS 时,函数名称会被修改,除非keep_fnames设置为true. 例如,以下打字稿代码:
class Test {}
console.log(Test.name);
Run Code Online (Sandbox Code Playgroud)
编译成JS:
function Test() {}
console.log(Test.name);
Run Code Online (Sandbox Code Playgroud)
将被丑化为:
function t() {}
console.log(t.name);
Run Code Online (Sandbox Code Playgroud)
并输出t而不是test到控制台。
有没有办法(除了使用keep_fnames选项)name在丑化后保留财产?(我不想使用,keep_fnames:true因为它会大大增加捆绑包的大小。
我想到的可能解决方案:
Test.name = 'Test',但这不会像Function.prototype.name只读属性那样工作;design:type元数据不是为类发出的,它只为属性发出(我相信这是因为Function.prototype.name存在,但我猜他们错过了这个边缘情况?)。我在玩Typescript装饰器,它们的行为似乎与与类继承一起使用时所期望的完全不同。
假设我有以下代码:
class A {
@f()
propA;
}
class B extends A {
@f()
propB;
}
class C extends A {
@f()
propC;
}
function f() {
return (target, key) => {
if (!target.test) target.test = [];
target.test.push(key);
};
}
let b = new B();
let c = new C();
console.log(b['test'], c['test']);
Run Code Online (Sandbox Code Playgroud)
哪个输出:
[ 'propA', 'propB', 'propC' ] [ 'propA', 'propB', 'propC' ]
Run Code Online (Sandbox Code Playgroud)
虽然我期望这样:
[ 'propA', 'propB' ] [ 'propA', 'propC' ]
Run Code Online (Sandbox Code Playgroud)
因此,似乎target.test在A,B和C之间共享。我对这里发生的情况的理解如下:
new B()触发A 的实例化,从而触发 …我试图canActivate使用RxJS observables在Angular 2/4中实现.我已经读过另一个关于这个问题了.使用以下代码,我的canActivate方法仅在应用程序启动时运行一次,但hello在isLoggedInobservable触发新值时永远不会再次打印.
canActivate(): Observable<boolean> {
return this.authService.isLoggedIn().map(isLoggedIn => {
console.log('hello');
if (!isLoggedIn) {
this.router.navigate(['/login']);
}
return isLoggedIn;
}).first();
}
Run Code Online (Sandbox Code Playgroud)
或者这个版本不能正常工作:
canActivate(): Observable<boolean> {
return this.authService.isLoggedIn().map(isLoggedIn => {
console.log('hello');
if (isLoggedIn) {
this.router.navigate(['/']);
}
return !isLoggedIn;
});
}
Run Code Online (Sandbox Code Playgroud)
但是,它适用于此代码:
canActivate(): Observable<boolean> {
return Observable.create(obs => {
this.authService.isLoggedIn().map(isLoggedIn => {
console.log('hello');
if (isLoggedIn) {
this.router.navigate(['/']);
}
return !isLoggedIn;
}).subscribe(isLoggedIn => obs.next(isLoggedIn));
});
}
Run Code Online (Sandbox Code Playgroud)
我在第一段代码中做错了什么?
编辑:这是isLoggedIn实施
@LocalStorage(AuthService.JWT_TOKEN_KEY)
private …Run Code Online (Sandbox Code Playgroud) 我正在尝试将其rewire与Karma(Webpack + Typescript)单元测试一起使用。我的单元测试是用Typescript编写的,与Webpack捆绑在一起,然后与Karma一起运行。我不断收到错误:
PhantomJS 2.1.1 (Windows 7 0.0.0) ERROR
Error: Cannot find module "module"
at src/myTest.spec.ts:187
Run Code Online (Sandbox Code Playgroud)
我查看了Rewire的代码,问题出在线路上
var Module = require("module"),
Run Code Online (Sandbox Code Playgroud)
我知道有一个Webpack Rewire插件,但是当我使用它时,我遇到的问题与Issue中已经报告的问题相同。
我所有不使用的测试都可以rewire正常工作。
这是我的测试文件:
import rewire = require("rewire");
const decorators = rewire("./decorators");
describe('something', () => {
it('should do something', () => {
decorators.__set__('Test', () => 'hello');
// In know this is pointless, but it's just to make sure that rewire works.
expect(decorators.Test).toBe('hello');
});
});
Run Code Online (Sandbox Code Playgroud)
这是我的webpack配置:
var webpack = require('webpack'); …Run Code Online (Sandbox Code Playgroud) 我想在当前期间结束时降级客户。当这种情况发生时。我知道的方法是将试用期设置proration_behavior为当前期末并设置为none,最后设置一个webhook来收听subscription.deleted然后创建新订阅1 2。我试图找到一种不使用 webhooks 的方法,以便 Stripe 处理他们身边的开关。
我认为这可以通过订阅计划以某种方式实现。
我有以下想法,但我还没有尝试过,我想先征求意见:
// create schedule if it doesn't exist
let schedule: Stripe.SubscriptionSchedule;
if (!stripeSubscription.schedule) {
schedule = await this.stripe.subscriptionSchedules.create({
from_subscription: stripeSubscription.id,
});
} else {
schedule = await this.stripe.subscriptionSchedules.retrieve(stripeSubscription.id);
}
await this.stripe.subscriptionSchedules.update(schedule.id, {
end_behavior: 'release',
phases: [
{
plans: [{plan: stripeSubscription.items.data[0].plan.id}],
end_date: stripeSubscription.current_period_end,
},
{
proration_behavior: 'none',
collection_method: 'charge_automatically',
plans: [{plan: replacementPlanId}],
end_date: stripeSubscription.current_period_end
},
],
});
Run Code Online (Sandbox Code Playgroud)
如果它不存在,上面将创建一个计划,然后更新该计划以降级计划。我不确定第二阶段结束是否真的有效,我可能需要增加一天才能确定。或者只是不设置任何内容并让计划发生,但我想知道在这种情况下是否会在第一阶段结束时将其删除,否则下次客户再次切换计划时,它可能会省略包括新的阵列中的计划。我可以通过执行以下操作来解决这个问题:
phases: [
...schedule.phases.filter(phase => phase.end_date > …Run Code Online (Sandbox Code Playgroud) 在 Materialise 中取消选中时,如何让复选框显示为“不确定”?
HTML:
<input type="checkbox" id="test" checked/>
<label for="test" id="test-label">Red</label>
Run Code Online (Sandbox Code Playgroud)
JS:
$('#test-label').click(function() {
var checkbox = $(this).prev();
if (!$(checkbox).is(':checked'))
$(checkbox).prop('indeterminate', true);
});
Run Code Online (Sandbox Code Playgroud)
typescript ×5
webpack ×2
angular ×1
checkbox ×1
decorator ×1
ecmascript-6 ×1
eslint ×1
inheritance ×1
javascript ×1
jestjs ×1
karma-runner ×1
materialize ×1
nestjs ×1
node.js ×1
observable ×1
reactjs ×1
rxjs ×1
subscription ×1
testing ×1
uglifyjs ×1